27 October 2017

One-liner for installing Microsoft Updates in PowerShell

The following one-liner is good for installing missing Microsoft Updates / Windows Updates.

First, Start PowerShell as administrator and then type:

Install-Module PSWindowsUpdate -Force ; Get-WUInstall –MicrosoftUpdate –AcceptAll –Install

The result will look like this:

image

05 September 2017

DHCP server migration between Windows Server 2008+ servers

Log on to old DHCP server, open administrative command prompt and type

netsh dhcp server export \\newserver\c$\dhcp.txt all

and stop and disable the dhcp server service on old server.

Then log on to new server, install DHCP service and authorize it

image

Then open administrative command prompt on new server and type

netsh dhcp server import \\newserver\c$\dhcp.txt all

or just

netsh dhcp server import c:\dhcp.txt all

If you are using DHCP relay in firewalls, then you might need to reconfigure them also to point to new server.

26 May 2017

How to manually add Java exception sites

 

Type in Start + Run window:

notepad %localappdata%low\sun\java\deployment\security\exception.sites

image

Then type all the addresses you want to open with Java without security error. It’s useful for example with Intel server motherboards.

image

11 May 2017

How to convert dynamic IP configuration to static using PowerShell

For example to start running DHCP or DNS server, you need to have static IP configuration. If you already have an DHCP Server configured IP configuration, you might want to use the same IP address and DNS configuration. To do this automatically, use this script:

$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | ogv –PassThru

#convert to static
$IPAddress = $nic.IPAddress[0]
$IPSubnet = $nic.IPSubnet[0]
$DefaultIPGateway = $nic.DefaultIPGateway
$DNSServerSearchOrder = $nic.DNSServerSearchOrder
$nic.EnableStatic($IPAddress, $IPSubnet)
$nic.SetGateways($DefaultIPGateway)
$nic.SetDNSServerSearchOrder($DNSServerSearchOrder)

#enable dhcp for ip and dns
$nic.EnableDHCP()
$nic.SetDNSServerSearchOrder()

First section is for selecting the correct network adapter. By running the second section, the current information is copied as static configuration. By running the third part, DHCP default configuration is set.

05 May 2017

How to remove empty folders using PowerShell

To remove empty folders from your D: drive, use following command:

$folders = gci d:\ -r | ? PSIsContainer -eq $True
$folders.Count
$emptyfolders = $folders | ? {$_.GetDirectories().Count -eq 0 -and $_.GetFiles().Count -eq 0}
$emptyfolders.Count
$emptyfolders | select * | ogv
$emptyfolders | Remove-Item

It has some room for improvement (i had to do multiple passes, because after cleaning up some other folders appeared empty (before they contained only empty folders inside it). And dont use it on C: drive, there might be useful empty folders.

01 May 2017

Schedule nightly restart using PowerShell

To register a nightly job:

Register-ScheduledJob restart {Restart-Computer -Force} -Trigger (New-JobTrigger -At 01:00 -Daily) -ScheduledJobOption (New-ScheduledJobOption -RunElevated)

To unregister the nightly job:

Unregister-ScheduledJob restart

To view jobs, use taskschd.msc (Task Scheduler) and go to \Microsoft\Windows\PowerShell\SceduledJobs folder and fine tune them if needed.

05 April 2017

Get screen resolutions and positions using PowerShell

To get screen resolutions and positions using powershell, you can use this oneliner:

Add-Type -A System.Windows.Forms;[System.Windows.Forms.Screen]::AllScreens|ft *d*

In my example I have this screen layout:

image

The output in my case is like this:

image

07 February 2017

How to bulk change Calendar permissions in Exchange 2013 using PowerShell

Let’s assume, that you want to let some group of users see each others calendars. Instead of instructing each user to change calendar permissions using Outlook, you can do it for them.

First lets try to see current permissions:

$users = Get-DistributionGroupMember groupname@contoso.com | select -ExpandProperty PrimarySmtpAddress
foreach ($user in $users)
{$folder = $user + ':\' + (Get-MailboxFolderStatistics $user | where foldertype -eq calendar).name
"------------------------------------------------------------------------------------------------"
$folder
Get-MailboxFolderPermission $folder
}


It will produce similar output:
image

To add permissions, you simply need to change the get cmdlet to add cmdlet:

$users = Get-DistributionGroupMember groupname@contoso.com | select -ExpandProperty PrimarySmtpAddress
foreach ($user in $users)
{$folder = $user + ':\' + (Get-MailboxFolderStatistics $user | where foldertype -eq calendar).name
"------------------------------------------------------------------------------------------------"
$folder

Add-MailboxFolderPermission $folder -User groupname@contoso.com -AccessRights reviewer
Set-MailboxFolderPermission $folder -User groupname@contoso.com -AccessRights reviewer
}

Add and Set are both present because if the permission is already present, then first command will fail. If you want to use default calendar permissions, the mailboxpermission cmdlets should have ‘-User Default’ in the middle. If you want to change calendar permissions on all users, then instead of get-distributiongroupmember you should use get-mailbox instead.