Tag Archives: Powershell

Office 365 Mailbox Folder Permissions in PowerShell

Set-executionpolicy unrestricted
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -allowclobber
Connect-MsolService -Credential $LiveCred

$Identity = "joe.bloggs@demo.org.uk:\Calendar"

#Remove-MailboxFolderPermission -Identity $Identity -User "A Bloke"

#Add-MailboxFolderPermission -Identity $Identity -User steve.bloggs@demo.org.uk -AccessRights Editor

Set-MailboxFolderPermission -Identity $Identity -User "Andrew Bloggs" -AccessRights Reviewer

Get-MailboxFolderPermission -Identity $Identity

Remote Disk Management

On the computer which you use to connect to the server core, run this command:
netsh advfirewall firewall set rule group="Remote Volume Management" new enable=yes

Or this powershell:
Enable-NetFirewallRule -name RVM-RPCSS-In-TCP,RVM-VDSLDR-In-TCP,RVM-VDS-In-TCP

….ON BOTH THE SOURCE AND TARGET MACHINES!!!

https://blogs.technet.microsoft.com/wincat/2012/05/02/what-needs-to-be-configured-to-do-remote-disk-management-of-a-server-core-installation-in-windows-server-2012-via-powershell/

https://social.technet.microsoft.com/Forums/windowsserver/en-US/22ca1d25-05b5-416c-bf9b-b301a2065d31/forum-faq-when-you-use-disk-management-to-connect-to-a-remote-computer-in-the-same-domain-you?forum=winserverfiles

Powershell Input Validation

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Input-Validation-30ebaa51

Function Validate-PostalCode
{
Param([Parameter(Mandatory=$true,HelpMessage="Enter a valid Postal Code xxx-xxx-xxxx")][ValidatePattern("[0-9][0-9][0-9][0-9]")]$PostalCode)
Write-host "The Pin Code $PostalCode is valid"
}

Function validate-PhoneNumber
{
Param([ValidatePattern("\d{3}-\d{3}-\d{4}")]$phoneNumber)
Write-host "The phone number $phoneNumber is valid"
}


validate-PhoneNumber -Phonenumber 999-999-9999

function Validate-Email ([string]$Email)
{
return $Email -match "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
}

function Validate-IPAddress ([string]$IP)
{
if($IP -match "(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})" -and -not ([int[]]$matches[1..4] -gt 255))
{
Write-host "The $IP IP is valid"
}
}

function Validate-Filename
{
Param([ValidatePattern("^\d{8}_[a-zA-Z]{3,4}_[a-zA-Z]{1}\.jpg"]$filename)
Write-host "The filename $filename is valid"
}

Live Migrations in PowerShell

If you get a hardware warning when attempting a live migration confirm that the Processor of the VM is configured, under Processor > Compatibility, to allow Migration to a different processor version. This is just a check box but the VM needs to be off to change it!

If you still get the error confirm that the virtual switch name on both Hyper-V boxes is the same. You will need to reboot the VM after you have updated them if you change the one on the Hyper-V host where the VM is currently.

Full powershell…

get-VM "VMtobeMoved" -ComputerName "NameofHyperVHost" | move-vm -DestinationHost "NameofDestinationHost" -VirtualMachinePath "D:\hyper-v\virtual machines\blah" -snapshotfilepath "D:\hyper-v\virtual machines\blah\" -SmartPagingFilePath "D:\hyper-v\virtual machines\blah\" -VHDs @(@{"sourcefilepath" = "c:\virtualmachines\blah\blah.vhd"; "destinationfilepath" = "D:\hyper-v\virtual hard disks\blah.vhd"}, @{"sourcefilepath" = "c:\virtualmachines\blah\virtual Hard Disks\blah-Data.vhd"; "destinationfilepath" = "D:\hyper-v\virtual hard disks\blah-Data.vhd"})

Hopefully you are sensible and all your Hyper-V files (machine, VHDs, snapshots, paging file, …) are all in one location. In which case you can use this much simpler script:

Get-VM "VMtobeMoved" -ComputerName "NameofHyperVHost" | Move-VM -DestinationHost "NameofDestinationHost" -IncludeStorage -DestinationStoragePath "D:\VMtobeMoved"

Getting fields from multiple Powershell Commands

—Single User—
$Mailbox = Get-MailBox "Joe Bloggs"
Get-mailboxstatistics "Joe Bloggs" | ft DisplayName,TotalItemSize,StorageLimitStatus,@{name = 'IssueWarningQuota';expression = {$Mailbox.IssueWarningQuota}},@{name = 'ProhibitSendQuota';expression = {$Mailbox.ProhibitSendQuota}},@{name = 'ProhibitSendRecieveQuote';expression = {$Mailbox.ProhibitSendRecieveQuota}}

—All Users—
$Mailbox = (Get-MailBox *)
foreach ($i in $Mailbox) {Get-mailboxstatistics $i.Name | ft DisplayName,TotalItemSize,StorageLimitStatus,@{name = 'IssueWarningQuota';expression = {$i.IssueWarningQuota}},@{name = 'ProhibitSendQuota';expression = {$i.ProhibitSendQuota}},@{name = 'ProhibitSendRecieveQuote';expression = {$i.ProhibitSendRecieveQuota}} }

—All Users Filtered—
$Mailbox = (Get-MailBox *)
foreach ($i in $Mailbox) {Get-mailboxstatistics $i.SAMAccountName | where {$_.StorageLimitStatus -ne "BelowLimit"} |ft DisplayName,TotalItemSize,StorageLimitStatus,@{name = 'IssueWarningQuota';expression = {$i.IssueWarningQuota}},@{name = 'ProhibitSendQuota';expression = {$i.ProhibitSendQuota}},@{name = 'ProhibitSendRecieveQuota';expression = {$i.ProhibitSendRecieveQuota}} }

—Tidy up formatting—
$Mailbox = (Get-MailBox *)
foreach ($i in $Mailbox) {
$info +=@(Get-mailboxstatistics $i.SAMAccountName | where {$_.StorageLimitStatus -ne "BelowLimit"} |select-object DisplayName,TotalItemSize,StorageLimitStatus,@{name = 'IssueWarningQuota';expression = {$i.IssueWarningQuota}},@{name = 'ProhibitSendQuota';expression = {$i.ProhibitSendQuota}},@{name = 'ProhibitSendRecieveQuota';expression = {$i.ProhibitSendRecieveQuota}} )
}
$info | ft -autosize