Solved

How to Execute Powershell script from the following code?

Posted on 2011-02-28
6
816 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now