Mark Minasi's Reader Forum
Mark Minasi's Reader Forum
Home | Profile | Register | Active Topics | Active Polls | Members | Search | FAQ | Minasi Forum RSS Feed
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 HALP! Questions on Windows and Windows Server
 PowerShell
 Stopping remote service on a workgroup server
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cbarnes
Here To Stay

USA
225 Posts
Status: offline

Posted - 07/24/2012 :  4:59:12 PM  Show Profile  Click to see cbarnes's MSN Messenger address  Reply with Quote
I'm trying to create script to run between two workgroup servers. I've tried to pass credentials through the administrator account (each admin account on different machines has a unique password). The script needs to execute from one server and stop the W3Svc, then restart the service on the other server. How do I do this? I am a PowerShell Moron...

Thanks! -cindy

glenn_faustino
Old Timer

Philippines
435 Posts
Status: offline

Posted - 07/25/2012 :  12:11:15 AM  Show Profile  Reply with Quote

Hi Cindy,

I've created an advanced function for this, please try it on test system first. For querying non-domain machine pass the credential like this: Stop-RemoteService -ServiceName wuauserv -ComputerName server1 -Credential localhost\Administrator -Verbose

function Stop-RemoteService {
[cmdletbinding()]
Param(
[parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)]
[string[]]$ComputerName,

[parameter(Mandatory=$true)]
[string]$Servicename,

[parameter()]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty
)
Begin {
$wmiparam = @{
NameSpace = 'root\cimv2'
Class = 'win32_service'
Filter = "name=`'$ServiceName`'"
ErrorAction = 'Stop'
}
if ($PSBoundParameters['Credential']) {
$wmiparam.Credential = $Credential
}
}
Process {
foreach ($computer in $ComputerName) {
$computer = $computer.ToUpper().Trim()
Write-Verbose ("Sending ping packet to {0} to determine if it's online." -f $computer)
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
Write-Verbose ("{0} is up, querying server using WMI." -f $computer)
$wmiparam.computername = $computer
try {
$ok = $true
$service = Get-WmiObject @wmiparam
}
catch {
$ok = $false
Write-Warning ("{0} : {1}`n" -f $computer,$($_.Exception.Message))
}
if ($ok) {
if ($service) {
Write-Verbose ("Stopping $servicename on $computer")
$service.StopService() | Out-Null
}
}
}
else {
Write-Warning ("{0} does not respond to ping!" -f $computer)
}
}
}
End {
Write-Host "`n>>> Script completed! <<<`n" -ForegroundColor Cyan
}
}


Hope this helps.

Glenn
Go to Top of Page

BubbaG1
Welcome Newcomer

USA
2 Posts
Status: offline

Posted - 07/25/2012 :  08:09:45 AM  Show Profile  Reply with Quote
I work with Cindy and asked her to post this while I was waiting on my authorization email...
Copied your code into the ISE and executed -- worked great, thanks!


Scott
Go to Top of Page

glenn_faustino
Old Timer

Philippines
435 Posts
Status: offline

Posted - 07/25/2012 :  08:16:39 AM  Show Profile  Reply with Quote

No worries Scott.

Btw, welcome to the forum
Go to Top of Page

BubbaG1
Welcome Newcomer

USA
2 Posts
Status: offline

Posted - 07/25/2012 :  10:22:53 AM  Show Profile  Reply with Quote
Thanks. All ready feeling the love!!! :D


Scott
Go to Top of Page

Mark Minasi
Chief cook and bottle washer

USA
10658 Posts
Status: offline

Posted - 07/27/2012 :  6:43:19 PM  Show Profile  Visit Mark Minasi's Homepage  Reply with Quote
And we so missed Ms. Barnes...<g>

Mark
tweetin' at mminasi
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Mark Minasi's Reader Forum © 2002-2011 Mark Minasi Go To Top Of Page
This page was generated in 0.19 seconds. Snitz Forums 2000