Something I need to keep for usage in the future:
$hostname=hostname
If (get-exchangeserver $hostname | where {$_.isClientAccessServer -eq $true})
{
} else {
}
If (get-exchangeserver $hostname | where {$_.isHubTransportServer -eq $true})
{
} else {
}
If (get-exchangeserver $hostname | where {$_.isMailboxServer -eq $true})
{
} else {
}
If (get-exchangeserver $hostname | where {$_.isUnifiedMessagingServer -eq $true})
{
} else {
}
If (get-exchangeserver $hostname | where {$_.isEdgeServer -eq $true})
{
} else {
}
Connecting to Exchange powershell is, for normal operations, as simple as opening the shortcut on you start menu :).
However, if you have the need to have some scripts perform actions against your Exchange you can use the below code to make that happen!
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://YourCASServerFQDN/PowerShell/ -Authentication Kerberos
Import-PSSession $s
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto
This function will download and install ,NET 4.0. It uses the Get-Framework-Versions function to determine if the installation is necessary or not. Internet Connectivity will be required as the script auto downloads the setup file (and sleeps for 360 seconds... I had a function in there to monitor for install completion at first, turns out the setup file spawns so many childprocesses the function just got confused and locked up -_-)
Alternatively you could drop the installation file in the folder specified on the $folderPath variable too. That will skip the download and use the file. This function easily adapts in to other versions f.e. I use it for Powershell 3 installs as well!
Function install-dotNet4 () {
if(($InstalledDotNET -eq "4.0") -or ($InstalledDotNET -eq "4.0c")){
write-host ".NET 4.0 Framework is already installed" -foregroundcolor Green
} else{
#set a var for the folder you are looking for
$folderPath = 'C:\Temp'
#Check if folder exists, if not, create it
if (Test-Path $folderpath){
Write-Host "The folder $folderPath exists." -ForeGroundColor Green
} else{
Write-Host "The folder $folderPath does not exist, creating..." -NoNewline -ForegroundColor Red
New-Item $folderpath -type directory | Out-Null
Write-Host " - done!" -ForegroundColor Green
}
# Check if file exists, if not, download it
$file = $folderPath+"\dotNetFx40_Full_x86_x64.exe"
if (Test-Path $file){
write-host "The file $file exists." -ForeGroundColor Green
} else {
#Download Microsoft .Net 4.0 Framework
Write-Host "Downloading Microsoft .Net 4.0 Framework..." -nonewline -ForeGroundColor DarkYellow
$clnt = New-Object System.Net.WebClient
$url = "http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe"
$clnt.DownloadFile($url,$file)
Write-Host " - done!" -ForegroundColor Green
}
#Install Microsoft .Net Framework
Write-Host "Installing Microsoft .Net Framework..." -nonewline -ForegroundColor DarkYellow
$dotNET4 = $folderPath+"\dotNetFx40_Full_x86_x64.exe /quiet /norestart"
Invoke-Expression $dotNET4
write-host " - done!" -ForegroundColor Green
start-sleep -seconds 360
}
}
This function will use the test-key function posted earlier. It will check which .NET frameworks are installed (currently only checking for .NET 4.0) but can be easily adapted and/or expanded.
function Get-Framework-Versions()
{
$installedFrameworks = @()
if(Test-Key "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Client" "Install") { $installedFrameworks += "4.0c" }
if(Test-Key "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full" "Install") { $installedFrameworks += "4.0" }
return $installedFrameworks
}
Disclaimer: Code is not mine but forgot where I got it. Ping me if this is yours and you will receive full kudos to it :)
function Test-Key([string]$path, [string]$key)
{
if(!(Test-Path $path)) { return $false }
if ((Get-ItemProperty $path).$key -eq $null) { return $false }
return $true
}
Disclaimer: This script is not of my own making. I found it on a share somewhere and it is so handy I started using in a bunch of scripts. To the writer:
If you're out there, somewhere, when you see this, thank you! Check if script is running as Adminstrator and if not use RunAs
# Use Check Switch to check if admin
param([Switch]$Check)
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()`
).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($Check) { return $IsAdmin }
if ($MyInvocation.ScriptName -ne "")
{
if (-not $IsAdmin)
{
try
{
$arg = "-file `"$($MyInvocation.ScriptName)`""
Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'
}
catch
{
Write-Warning "Error - Failed to restart script with runas"
break
}
exit # Quit this session of powershell
}
}
else
{
Write-Warning "Error - Script must be saved as a .ps1 file first"
break
}
write-host "Script Running As Administrator" -foregroundcolor red
Write-host ""