Link to home
Start Free TrialLog in
Avatar of missymadi
missymadi

asked on

How to Execute Powershell script from the following code?

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

Avatar of Dale Harris
Dale Harris
Flag of United States of America image

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
Avatar of missymadi
missymadi

ASKER

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
}
}
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
ASKER CERTIFIED SOLUTION
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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! :)
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