Create AD users from a CSV-file

Here is an example of a script to create AD users from a CSV-file and assign a temoporary password.

param
(
	[parameter(Mandatory = $true)]
	$infile,
	$output
)

$OU = "OU=Users,DC=PSLABB,Dc=local"

$users = Import-Csv -Path $infile


foreach ($user in $users)
{
	$rand = Get-Random -Minimum 1000 -Maximum 9999
	$pwd = "Temp$rand"
	$name = "$($user.givenname)" + " " + $($user.surname)
	
	New-ADUser -Name $name -GivenName $user.Givenname -Surname $user.Surname -SamAccountName $user.username -user $user.username -Description "Tempuser" -path $ou -enabled $true -AccountPassword (convertTo-securestring -AsPlainText "Temp$rand" -Force)
	
	$details = @{
		'Name'= $name;
		'username'  = $($user.username);
		'Password'     = $pwd
	}
	$results = New-Object PSObject -Property $details
	$results | Export-Csv -Path $output -NoTypeInformation -Append
	
}

Fix VMware tools not running on Windows servers

I had some trouble with VMwaretools stopping on some machines and decided to automate restart och vmware tools. I wrote this script which takes a view parameter to view current status. If you start the script whitout the view parameter it will try to restart vmware tools.

fix-toolsnorunning.ps1

param
(
[parameter(Mandatory = $false)]
[switch]
$view
)

#Connect to Vcenter
$cred = Get-Credential
Connect-VIServer vcenter -Credential $cred | Out-Null

Write-Host -ForegroundColor Green "Collecting VMs"
$toolsvm=get-vm | where { $_.GuestId -like "*Windows*" -and $_.Extensiondata.Summary.Guest.ToolsStatus -like "toolsnotrunning" -and $_.Powerstate -eq "PoweredOn"}

#Stop script if no VMs reports tools not running
if ($toolsvm -eq $null)
{
Write-Host "Nothing to fix"
Disconnect-VIServer vcenter -Confirm:$false
exit
}

if ($view)
{
Write-Host "VMs whitout tools running"
foreach ($vm in $toolsvm)
{
Write-Host $vm
}
}
Else
{

foreach ($vm in $toolsvm)
{

if ($vm -match "^(?<Name>\S*)\s.*$") { $vm_name = $Matches.Name }
else { $vm_name = $vm }

$vm_hostname = $vm_name.name +".domain.local"

if (Test-Connection -Computername $vm_hostname -BufferSize 16 -Count 1 -Quiet)
{
Invoke-Command -Computername $vm_hostname -ScriptBlock { stop-Service -name vmvss; start-service vmvss } -Credential $cred
Write-Host "Service restarted on $vm_name"
}
Else
{
Write-Host -ForegroundColor Red "Unable to contact $vm_name"
}

}
}
#Disconnect from Vcenter.
Disconnect-VIServer vcenter -Confirm:$false

OP5 plugin example in powershell

If you want to monitor your windows server uptime in OP5. Run from NSClient++ on all hosts you want to monitor uptime on.

param
(
	[parameter(Mandatory = $true)]
	[int]$w,
	[parameter(Mandatory = $true)]
	[int]$c
)
function Get-Uptime
{
	$os = Get-WmiObject win32_operatingsystem
	$uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
	return $Uptime
}
$os = Get-CimInstance Win32_OperatingSystem | Select-Object  Caption | ForEach{ $_.Caption }
$uptime = Get-Uptime

if ( $uptime.Days -gt $c)
{
	Write-Host Write-Host "CRITICAL Uptime:" $Uptime.Days "days"
	exit 2

}
if ($uptime.Days -gt $w)
{
	Write-Host Write-Host "WARNING Uptime:" $Uptime.Days "days"
	exit 1

}
if ($uptime.Days -lt $w)
{
	Write-Host "OK" $Uptime.Days "days uptime"
	Write-Host "OS: $os"
	exit 0
}

Create a new host via OP5 API with powershell

If you need to create a new host with OP5 api you can use something like this.
Template, hostgroups and contactgroups are not mandatory and can be removed from hash table.

 

function new-op5host
(
	[parameter(Mandatory = $true)]
	[string]$hostname,
	[parameter(Mandatory = $true)]
	[string]$alias
)
{
	$username = 'api_user$Default'
	$password = "password"
	
	$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
	
	$hash = @{
		file_id = "etc/hosts.cfg";
		host_name = "$hostname";
		address = "$hostname.domain.com";
		alias = "$alias";
		max_check_attempts = "3";
		notification_interval = "5";
		notification_options = "d", "r";
		notification_period = "24x7";
		template = "Win_hosts";
		hostgroups = "Windows servers";
		contact_groups = "Windowsservrar"
	}
	$jsonData = $hash | convertto-json
	
	$result = Invoke-RestMethod -Method POST -ContentType "application/json" -Uri "https://op5.domain.com/api/config/host" -Body $jsonData -Headers @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
	
	#Save
	$result = Invoke-RestMethod -Method POST -ContentType "application/json" -Uri "https://op5.domain.com/api/config/change" -Headers @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
		
}

Change wallpaper on windows 7

I needed a script to change desktop wallpaper for all users using the default one with an outdated company logo on. I came up with this startup script as a solution. Set $filesize to the size of the old wallpaper to only change wallpapers with same filesize. Use as logon script.

$filesize = 100593
$bg = Get-Item ("$env:USERPROFILE\appdata\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg)
if ($bg.length -eq $filesize)
{
Copy-item "\\fileserver\UsrLogon\wallpaper\img0.jpg" "$env:USERPROFILE\appdata\Roaming\Microsoft\Windows\Themes\img0.jpg" -Force
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value "$env:USERPROFILE\appdata\Roaming\Microsoft\Windows\Themes\img0.jpg"
rundll32.exe user32.dll, UpdatePerUserSystemParameters
}