Skip to main content

Posts

Showing posts from 2016

Powershell: Change UPN on list of users

I needed a script to bulk change a list of users to a new UPN i came up with this. # $Users = gc c:\temp\users.txt | get-ADUser foreach ($User in $Users) $UserUPN = $User.UserPrincipalname $UserUPNwithOutDomain = ([regex]::matches($UserUPN, "([^@]+)")).value[0] Set-ADUser $User -userprincipalname "$UserUPNwithOutDomain@microsoft.com" }

Powershell: Get services and accounts used to run them on all computers using Jobs

cls #################### #By Tony Unger #Purpose: inventory all services and user accounts running them with jobs # ## $myCol = @() $i =0 $servers = get-adcomputer -filter * | select -expand name $scriptblock = { param([string]$server) get-wmiobject win32_service -computername $Server | select * } $ServersCount = $Servers.count foreach($server in $Servers){ $i++ $running = @(Get-Job | Where-Object { $_.State -eq 'Running' }) Write-Progress -Activity "Gathering computer info" -status "Currently on $server -- $i of $ServersCount" -percentComplete ($i / $ServersCount*100) if ($running.Count -le 50) { Start-Job -ScriptBlock $scriptblock -Name $server -ArgumentList $server, $creds } else { $running | Wait-Job } } while ($running.Count -ge 1) { sleep 1 $running = @(Get-Job | Where-Object { $_.State -eq 'Running' }) } foreach ($job in Get-Job ){ $recjobs = Receive-Job -Keep -Job $job fore

Powershell: RDP log user account off of all servers using jobs.

#Log User account off all servers # Tony Unger # $Domain = "Microsoft.com" $Servers = Get-ADComputer -Filter {(OperatingSystem -Like "Windows Server*")-and (enabled -eq "true")} -Property SamAccountName -Server $Domain Select -expand Name $username = read-host "Enter Users Name:" $i = 0 $scriptblock = { param( [string]$server, $Username ) $queryResults = (qwinsta /server:$server $Username| foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv) foreach ($queryResult in $queryResults){ Logoff /server:$server $queryResult.id } } $TotalServersCount = $Servers.count foreach($server in $Servers){ $i++ $running = @(Get-Job | Where-Object { $_.State -eq 'Running' }) Write-Progress -Activity "logging off user" -status "Currently on $server -- $i of $TotalServersCount" -percentComplete ($i / $Servers.count*100) if ($running.Count -le 50) { Start-Job -ScriptBlock $scriptblo