Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Execute Powershell script from the following code?

Posted on 2011-02-28
6
Medium Priority
?
825 Views
Last Modified: 2012-05-11
Experts,

    What is the syntax to execute another Powershell script from the script below? I want the user to enter a computer name. Based on the computer name - the specified reset password script should run for the "foreach"
 
Thanks, Missymadi
$strComputer = Read-Host "Enter Computer Name"
$OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" `
-ComputerName $strComputer

# if statement to run code for Windows XP and Windows 2003 Server.
if (($OS.Version -eq "5.1.2600") -or ($OS.Version -eq "5.2.3790"))
{
write-host "Computer Name: " $strComputer
#nested if statement
if ($OS.Version -eq "5.1.2600") {write-host "OS Version: Windows XP"}
elseif ($OS.Version -eq "5.2.3790") {write-host "OS Version: Windows 2003"}
$colPrinters = Get-WmiObject -Class win32_Printer -namespace "root\CIMV2" `
-computerName $strComputer
foreach ($objPrinter in $colPrinters) {
write-host "Name: " $objPrinter.Name
write-host "Description: " $objPrinter.Description
write-host
}
}

Open in new window

0
Comment
Question by:missymadi
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 35000816
Just add this

If your script name is "Script1" and located in the same directory:

.\Script1.ps1

If it's in a different directory, do this:

C:\Script1.ps1

Or wherever it lies.

HTH,

Dale Harris
0
 

Author Comment

by:missymadi
ID: 35001033
Is this correct?

$strComputer = Read-Host "Enter Computer Name"
$OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" `
-ComputerName $strComputer

# if statement to run code for Windows XP and Windows 2003 Server.
if (($OS.Version -eq "5.1.2600") -or ($OS.Version -eq "5.2.3790"))
{
write-host "Computer Name: " $strComputer
#nested if statement
if ($OS.Version -eq "5.1.2600") {write-host "OS Version: Windows XP"}
.\Script1.ps1
elseif ($OS.Version -eq "5.2.3790") {write-host "OS Version: Windows 2003"}
.\Script1.ps1
write-host "Description: " $objPrinter.Description
write-host
}
}
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 35001067
Not quite:

$strComputer = Read-Host "Enter Computer Name"
$OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" `
-ComputerName $strComputer

# if statement to run code for Windows XP and Windows 2003 Server.
if (($OS.Version -eq "5.1.2600") -or ($OS.Version -eq "5.2.3790"))
{
write-host "Computer Name: " $strComputer
#nested if statement
if ($OS.Version -eq "5.1.2600") {
write-host "OS Version: Windows XP"
.\Script1.ps1
}
elseif ($OS.Version -eq "5.2.3790")
{
write-host "OS Version: Windows 2003"
.\Script1.ps1
}
write-host "Description: " $objPrinter.Description
write-host
}
}

HTH,

DH
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 16

Accepted Solution

by:
Dale Harris earned 2000 total points
ID: 35001102
Also, if I may offer a simpler script:

Let's say there are only two outcomes: XP and 2003.  Then you could use something like this:

$strComputer = Read-Host "Enter Computer Name"
$OS = gwmi win32_OperatingSystem -Comp $strComputer
# if statement to run code for Windows XP and Windows 2003 Server.
write-host "Computer Name: " $strComputer
if ($OS.Version -eq "5.1.2600") {
write-host "OS Version: Windows XP"
.\Script1.ps1
}
else
{
write-host "OS Version: Windows 2003"
.\Script1.ps1
}
#What is the next line for?
write-host "Description: " $objPrinter.Description
}#end if


HTH,

Dale Harris
0
 

Author Comment

by:missymadi
ID: 35001179
My goal is to  test for Windows OS's and running a Powershell script to reset AD users passwords.

 Then after I get that working I was going to add a test for Linux box. I thought I could have the user enter a computer name then test to see if it is "pingable" then test to see if port 22 is open - this would verify that it is a Linux box. BUT I also need to include a line to test if the SSH service is running, if not turn it on. I welcome any suggestions! :)
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 35001353
I would recommend a different question being opened up to allow for different solutions posted by Linux users.  As far as this question goes, you should be good to start using that for your Server 2003 and XP boxes.

Happy Scripting!

Dale Harris
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Loops Section Overview
Screencast - Getting to Know the Pipeline

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question