Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
$config = @{
SubscriptionName = "";
VMName = ""
}
$myip = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content
Write-Host "My Public IP is", $myip;
if (-Not(Get-InstalledModule -Name "Az")) {
Write-Host "Installing Azure Package from PS Gallery...";
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force;
}
if (-Not(Get-Module -Name "Az") -and -Not(Get-Command -Name Get-AZContext)) {
Write-Host "Importing Azure Module..."
Import-Module -Name Az -Force;
}
if (-Not (Get-AZContext)) {
Write-Host "Connecting to Azure..."
Connect-AzAccount #-Credential $azCredF
}
if ([bool]((Get-AZContext))) { Write-Host "Connected to Azure using :", ((Get-AZContext).Account) }
else { Write-Error "Failed to connect to Azure"; return; }
if (((Get-AZContext).Subscription.Name) -ne $config.SubscriptionName) {
Write-Host "Switching Subscription Context...";
(Get-AzSubscription -SubscriptionName $config.SubscriptionName) | Set-AzContext | Out-Null;
}
Write-Host "Subscription =", ((Get-AZContext).Subscription.Name);
$vm = Get-AzVM -VMName $config.VMName
Write-Host "Located Azure VM '$($vm.Name)' within '$($vm.ResourceGroupName)' ResourceGroup";
$nic = $vm.NetworkProfile.NetworkInterfaces;
$networkinterface = ($nic.id -split '/')[-1];
$nicdetails = Get-AzNetworkInterface -Name $networkinterface;
$nsg = Get-AzNetworkSecurityGroup -Name (($nicdetails.NetworkSecurityGroup.Id -split '/')[-1]) -ResourceGroupName ($vm.ResourceGroupName)
# $rules = Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup
# $rdpRule = $rules | Where-Object { $_.Protocol -eq 'TCP' -and $_.DestinationPortRange -contains 3389 }
Write-Host "Before Updating public ip";
($nsg.SecurityRules | Where-Object { $_.Name -eq "RDP" }).SourceAddressPrefix
Write-Host ("*" * 10)
($nsg.SecurityRules | Where-Object { $_.Name -eq "RDP" }).SourceAddressPrefix = ([System.String[]] @("xxx.xxx.xxx.xxx", "xxx.xxx.xxx.xxx", $myip))
$nsg | Set-AzNetworkSecurityGroup | Get-AzNetworkSecurityRuleConfig -Name "RDP" | Out-Null
Write-Host "After Updating public ip";
($nsg.SecurityRules | Where-Object { $_.Name -eq "RDP" }).SourceAddressPrefix
Disconnect-AzAccount | Out-Null
PowerShell REST API Programming
PowerShell Code Snippet
Strip-Word
Function Strip-Word {
Param (
[Parameter(mandatory=$true,Position=1)][string]$word,
[Parameter(mandatory=$true,Position=2)][int]$length,
[Parameter(mandatory=$false,Position=3)][AllowNull()][switch]$encode
)
$word = $word -replace '[^a-zA-Z0-9-\(\)_ ]', ''
if($encode) {
return [System.Web.HttpUtility]::UrlEncode($word.PadRight($length,' ').Substring(0,$length).Trim());
} else {
return $word.PadRight($length,' ').Substring(0,$length).Trim();
}
}
Test-ADCredential
# Test-ADCredential.ps1
CLS
<#
.Synopsis
Verify Active Directory credentials
.DESCRIPTION
This function takes a user name and a password as input and will verify if the combination is correct. The function returns a boolean based on the result.
.NOTES
Name: Test-ADCredential
Author: Jaap Brasser
Version: 1.0
DateUpdated: 2013-05-10
.PARAMETER UserName
The samaccountname of the Active Directory user account
.PARAMETER Password
The password of the Active Directory user account
.EXAMPLE
Test-ADCredential -username jaapbrasser -password Secret01
Description:
Verifies if the username and password provided are correct, returning either true or false based on the result
#>
function Test-ADCredential {
[CmdletBinding()]
Param
(
[string]$UserName,
[string]$Password
)
if (!($UserName) -or !($Password)) {
Write-Warning 'Test-ADCredential: Please specify both user name and password'
} else {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
$DS.ValidateCredentials($UserName, $Password)
}
}
try{ $testADCredential = $null; $testADCredential = Get-Credential } catch { $testADCredential = $null; }
if($testADCredential -ne $null) { Test-ADCredential -UserName "$($testADCredential.UserName)" -Password "$($testADCredential.GetNetworkCredential().password)"; }
pause; return;
Active Directory account locking out after password reset
Several end-users account locking out after recently resetting their domain password.
Our domain policy is lockout Threshold 3 attempts. They are getting locked out after 1 try. Once the user logs in, any network or domain resources they try to get to prompt them for their credentials because they are locked out. Their domain account shows locked in AD as well.
Continue reading “Active Directory account locking out after password reset”Updating the Windows Console Colors
$Host.UI.RawUI.BackgroundColor = ($bckgrnd = 'Black')
$Host.UI.RawUI.ForegroundColor = 'White'
$Host.PrivateData.ErrorForegroundColor = 'Red'
$Host.PrivateData.ErrorBackgroundColor = $bckgrnd
$Host.PrivateData.WarningForegroundColor = 'Magenta'
$Host.PrivateData.WarningBackgroundColor = $bckgrnd
$Host.PrivateData.DebugForegroundColor = 'Yellow'
$Host.PrivateData.DebugBackgroundColor = $bckgrnd
$Host.PrivateData.VerboseForegroundColor = 'Green'
$Host.PrivateData.VerboseBackgroundColor = $bckgrnd
$Host.PrivateData.ProgressForegroundColor = 'Yellow'
$Host.PrivateData.ProgressBackgroundColor = $bckgrnd
$Host.PrivateData.ConsolePaneForegroundColor = 'DarkCyan'
$Host.PrivateData.ConsolePaneBackgroundColor= $bckgrnd
$Host.PrivateData.ConsolePaneTextBackgroundColor= 'Yellow'
Set-PSReadlineOption -ResetTokenColors
Clear-Host
dbatools: Migrate SQL Server Logins Between Instances
Uses PowerShell and SMO to migrate SQL logins. Logins are completely migrated and maintain their SIDs, passwords, server/database roles, server/database permission sets & securable, default database and login attributes. Works on SQL Server 2000 and above.
dbatools.io: PowerShell & SQL Server
PowerShell: Script to move items from one folder to another in a user’s office 365 mailbox
Exchange Web Services 2.0 Using PowerShell
- Exchange Online and Exchange 2013 development
- Microsoft Exchange Web Services Managed API 2.2
- EWS Managed API reference
- PowerShell – Script to move items from one folder to another in a user’s mailbox
- Quick searching of today’s email using Powershell and EWS
- Search-Mailbox
- Explore the EWS Managed API, EWS, and web services in Exchange
- EWS Managed API, EWS, and web services in Exchange
- Exchange Online and Exchange 2013 development
- Exporting and importing items by using EWS in Exchange
- PowerShell EWS Save as for e-mails
PowerShell implicit remoting on imported a cmdlet
Import-PSSession -Session (
New-PSSession -ComputerName dc01 -Credential (Get-Credential)
) -CommandName New-ADUser
$Password = Read-Host -assecurestring "SP2013 Farm Account Password"
$Name = "spExtranetFarm"
$UPN = "spExtranetFarm@mikefrobbins.com"
$Description = "SharePoint Farm Administrator Account - Extranet"
$Path = "ou=service,ou=accounts,ou=test,dc=mikefrobbins,dc=com"
New-ADUser -Name $Name -AccountPassword $Password -Description $Description `
-Enabled $true `
-PasswordNeverExpires $true `
-Path $Path `
-SamAccountName $Name `
-UserPrincipalName $UPN