30. november 2011

Start, stop and restart services on remote machines

Greetings!

I would like to share a couple of nifty commandlets that i build into out costume module. 
Start-RemoteService, Stop-RemoteService and Restart-RemoteService.

This is particular nice when PrintServer104 is troubeling you and you need to restart spooler.  Easy task, but normally you would either log onto the server and fire up whatever favorite way to stop a service or on a remote server start services.msc and remote connect to a the printserver, find the service and restart it.

With these commandlets it have become faster and easier.  In your favorite prompt (assuming that is a Powershell prompt) type

Restart-RemoteService -computer PrintServer104 -service spooler

You can also just choose to start a searvice by

Start-RemoteService -computer PrintServer104 -service spooler

or stop a service by

Stop-RemoteService -computer PrintServer104 -service spooler

Cut and past these functions into your custom module and save time and frustrations by getting the job done :)

Function Start-RemoteService
{
<#
 .Synopsis
 Send a command to remote server to start a specifik service
           
 .Description
 Send a command to remote server to start a specifik service
           
 .Parameter $Computer
 Computername
 
 .Parameter $service
 Service to restart
 
 .Example
 Start-RemoteService Server01 spoolsv
           
 Description
 -----------
 Will start the service named spoolsv on server Server01
    
 .Notes
 NAME:      Start-RemoteService
 AUTHOR:    Claus Søgaard
#>
 [CmdletBinding()]
 param([string]$strComputer,[String]$strService)
 Process
 {
   $result=(Get-WmiObject -computer $strComputer Win32_Service -Filter "Name='$strService'").InvokeMethod("startService",$null)
  if($result -eq "0")
  {
    Write-Host "Succesfully started $strService"
  }
  ELSE
  {
    Write-Host -ForegroundColor Red "Failed to start $service on $strComputer"
  }
 }
}



Function Stop-RemoteService
{
<#
 .Synopsis
 Send a command to remote server to stop a specifik service
           
 .Description
 Send a command to remote server to stop a specifik service
           
 .Parameter $Computer
 Computername
 
 .Parameter $service
 Service to stop
 
 .Example
 Stop-RemoteService Server01 spoolsv
           
 Description
 -----------
 Will stop the service named spoolsv on server Server01
    
 .Notes
 NAME:      Stop-RemoteService
 AUTHOR:    Claus Søgaard
#>
 [CmdletBinding()]
 param([string]$strComputer,[String]$strService)
 Process
 {
   $result=(Get-WmiObject -computer $strComputer Win32_Service -Filter "Name='$strService'").InvokeMethod("stopService",$null)
  if($result -eq "0")
  {
    Write-Host "Succesfully stopped $service on $strComputer"
  }
  ELSE
  {
    Write-Host -ForegroundColor Red "Failed in stopping $strService"
  }
 }
}



Function Restart-RemoteService
{
<#
 .Synopsis
 Send a command to remote server to restart a specifik service
           
 .Description
 Send a command to remote server to restart a specifik service
           
 .Parameter $Computer
 Computername
 
 .Parameter $service
 Service to restart
 
 .Example
 Restart-RemoteService Server01 spoolsv
           
 Description
 -----------
 Will restart the service named spoolsv on server Server01
    
 .Notes
 NAME:      Start-RemoteService
 AUTHOR:    Claus Søgaard
#>

 [CmdletBinding()]
 param([Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string]$Computer,
       [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][String]$service)
 Process
 {
   Stop-RemoteService $computer $service
  Start-Sleep 1
  Start-RemoteService $computer $service
 }
}

24. november 2011

Delete cookies

Hello,
Just had a user calling me telling me that on a specific site where the user needs to log in and specify a location to where his documents are, it shows an invalid path to the old fileserver. 
Ruling out any GPO, i turned to cookies.  Since we never delete any cookies for the users, this one have had thousands og cookies!
I want to find cookies where this site have saved som informations and i just need the job done...

declaring the path to cookies (which is on a fileserver)
$path=\\Fileserver\svprofil$\System\SVJVI\Citrix\H5\cookies

Now get all the cookies
$cookies=Get-ChildItem $path

now find the cookies that contains the specific site$matchCookies=$cookies | Select-String -Pattern "<specific site>"


$matchCookies | gm reveals that

   TypeName: Microsoft.PowerShell.Commands.MatchInfo
Name         MemberType Definition
----         ---------- ----------
Equals       Method     bool Equals(System.Object obj)
GetHashCode  Method     int GetHashCode()
GetType      Method     type GetType()
RelativePath Method     string RelativePath(string directory)
ToString     Method     string ToString(), string ToString(string directory)
Context      Property   Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename     Property   System.String Filename {get;}
IgnoreCase   Property   System.Boolean IgnoreCase {get;set;}
Line         Property   System.String Line {get;set;}
LineNumber   Property   System.Int32 LineNumber {get;set;}
Matches      Property   System.Text.RegularExpressions.Match[] Matches {get;set;}
Path         Property   System.String Path {get;set;}
Pattern      Property   System.String Pattern {get;set;}


in order to remove a file we need the entire path.  We dont have fullname in out members, but we do have filename.  I use the path from before and melt it with the attribute filename.

$cookies | Select-String -Pattern "danskebank" | foreach{"$path\"+$_.filename}
file://fileserver/profileShare$/System/Username/Citrix/H/cookies/BJ9NTMU2.txt
file://fileserver/svprofil$/System/SVJVI/Citrix/H/cookies/MFBOIQR5.txt
file://fileserver/svprofil$/System/SVJVI/Citrix/H/cookies/8VN8Y5A5.txt


now its just to implement remove-item:

$cookies | Select-String -Pattern "danskebank" | foreach{remove-item ("$path\"+$_.filename)}

jobs done :)

Fixing users UserPrincipalName

We have had a scenario where some users lacked the attribute UserPrincipalName. 
To find these users I used:


$users=Get-ADUser -Server <DomainController> -filter * -searchbase "OU=Users,OU=Hosting,DC=<subdomain>,DC=<rootDomain>,DC=dk" | where {$_.userPrincipalName -eq $null}

Now to fix the attribute:


$users | foreach{$_.samaccountname;$_.UserPrincipalName = ($_.samaccountname+"@subdomain.rootdomain.dk");set-aduser -instance $_}

You can question the readability of this, but it gets the job done which is what we want.

Best regards
Claus