Solved

How to Execute Powershell script from the following code?

Posted on 2011-02-28
6
819 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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