Solved

How to Execute Powershell script from the following code?

Posted on 2011-02-28
6
817 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

20 Experts available now in Live!

Get 1:1 Help Now