Powershell – Graphical Tools Cmdlets

Out-GridView gives a dynamic table view of results in a pipeline with sorting and filtering capabilities and when used with -PassThru can be used interactively within a pipeline to select objects to send back to the pipeline.

PS c:\>Get-ADComputer -Filter * | out-gridview -PassThru | select name,enabled
name enabled
---- -------
PC1 False
PC2 True
PC3 True

Show-Command gives a graphical view of a command including parameter sets, parameters, switches, etc…

Get-Help -ShowWindow works again to give a graphical view of PowerShell help content.

Powershell – Exchange – enable/disable junk mail rule in a mailbox

Set-MailboxJunkEmailConfiguration -Enabled <$true | $false>

To check:

Get-MailboxJunkEmailConfiguration

To disable the junk email rule on all mailboxes in Exchange:

$All = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$All | foreach {
Set-MailboxJunkEmailConfiguration $_.Name -Enabled $false
}

Powershell – DNS lookup

param (
[Parameter(Mandatory=$true)]
[string]$HostName
)
$output = $null
$output = @()
$DNSServers = Get-ADDomainController -Filter *
$DNSServers | foreach {
$query = Resolve-DnsName -Name $hostname -Server $_.HostName
$result = New-Object psobject -Property @{
dnsserver = $_.Name
hostname = $query.name
IPAddress = $query.ipaddress
}
$output += $result
}
$output | select hostname,ipaddress,dnsserver | sort ipaddress

save it as for example: dns-lookup.ps1 then

PS C:\Scripts> .\dns-lookup.ps1 server2016
hostname IPAddress dnsserver
-------- --------- ---------
server2016.domain.local 192.168.1.53 DC_01
server2016.domain.local 192.168.1.53 DC_02
server2016.domain.local 192.168.1.53 DC_09

PowerShell – Set VM permissions

Here is my PS way of granting user access to a VM in vSphere

$vmname = read-host "Enter Virtual Machine name"
$username = read-host "Enter User name (i.e. DOMAIN\username)"
$roles = Get-VIRole | select name | % {$counter = -1} {$counter++; $_ | Add-Member -Name Role_ID -Value $counter -MemberType NoteProperty -PassThru}
$roles | ft -auto
$myRole = read-host "select Role ID"
$role_selection = $roles[$myRole]
get-vm $vmname | New-VIPermission -Role (Get-VIRole -Name $role_selection.name) -Principal $username

PowerShell – Run script in new console window

Have you ever had to run powershell script in a new console window with your for example admin account?

param (
[Parameter(Mandatory=$true)]
[string]$command
)
runas /user:domain\user_name "powershell.exe -noExit -command $command"

I have that saved as “run-powershel_command.ps1

usage:
run-powershell_command.ps1 "get-aduser bob"

this will ask you for the password for the hardcodded username once that is provided it will open new window and execute the command.