Wednesday, April 16, 2014

I just ran into a case where I needed to remove the PC side client from BMC TrackIt! from an entire domain.  I created a little script to do it for me.  It happily ran through and removed it from every workstation.  It's quick and dirty but it does work.


clear-host

function Run-RemoteCMD {
    param(
    [Parameter(Mandatory=$true,valuefrompipeline=$true)]
    [string]$computer,
    [string]$command
    )
    begin {
    #$computer
        #$command = "ipconfig /registerdns" #Read-Host " Enter command to run"
        [string]$cmd = "CMD.EXE /C " +$command
                        }
    process {
        $newproc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $computer
        sleep -Seconds 1
        if ($newproc.ReturnValue -eq 0 )
                { Write-Output " Command $($command) invoked Sucessfully on $($computer)" }
                # if command is sucessfully invoked it doesn't mean that it did what its supposed to do
                #it means that the command only sucessfully ran on the cmd.exe of the server
                #syntax errors can occur due to user input
    }
}
      
$servers = Get-ADComputer -filter {*}

foreach ($server in $servers){
    Write-Host "========================================"
    $pc = $server.name
    if (Test-Connection -ComputerName $pc){
    Write-Host $pc
#------------------------------------------------------
    if (get-service -ComputerName $pc -Name TIRmtSvc){
        Write-Host "service found on $pc" -ForegroundColor yellow
        (get-service -ComputerName $pc -Name TIRmtSvc).Stop()
        While (!((Get-Service -ComputerName $pc -Name TIRmtSvc).Status = "Stopped")){
            sleep -Seconds 2
        }
    }else{
    Write-Host "service not found on $pc" -ForegroundColor cyan
}
#-------------------------------------------------------
    if (test-path -Path "\\$pc\c$\Windows\tiremote" ){
        Write-Host "folder found on $pc" -ForegroundColor yellow
        Run-RemoteCMD $pc "c:\Windows\tiremote\TIREMOTE.EXE /UNINSTALL -w 100000 > nul"
        sleep -Seconds 5
        if (get-service -ComputerName $pc -Name TIRmtSvc){
            Write-Host "service removed from $pc" -ForegroundColor green
        }else{
            Write-Host "service NOT removed from $pc" -ForegroundColor red
        }
        Run-RemoteCMD $pc "rd c:\Windows\TIREMOTE /s /q"
        if (test-path -Path "\\$pc\c$\Windows\tiremote" ){
            Write-Host "folder removed from $pc" -ForegroundColor green
        }else{
            Write-Host "folder NOT removed from $pc`ntrying again" -ForegroundColor red
            sleep -Seconds 5
            Run-RemoteCMD $pc "rd c:\Windows\TIREMOTE /s /q"
            if (test-path -Path "\\$pc\c$\Windows\tiremote" ){
                Write-Host "folder removed from $pc" -ForegroundColor green
            }else{
                Write-Host "folder NOT removed from $pc" -ForegroundColor red
            }
        }
    }else{
        Write-Host "folder not found on $pc" -ForegroundColor cyan
}
#---------------------------------------------------------
      
}else{
    Write-Host "no connection to $pc" -ForegroundColor Red  
}
}
Write-Host "completed"   

No comments:

Post a Comment