Posts tagged Powershell

Getting Directory Sizes in PowerShell

0

I had the problem to get all sizes of our user home directories and found an article from the scripting guys which can be found here.

And you can download the script via following site

http://gallery.technet.microsoft.com/scriptcenter/Outputs-directory-size-964d07ff

Check if a service is running via PowerShell

0

I´ve found a script in another blog by Anders Mikkelsen which I found really good and quite helpful since I´ve experienced that different services on some machines have the habit not to start after the server is being rebooted because of an update or reboot cycle at night.

function FuncCheckService{
    param($ServiceName)
    $arrService = Get-Service -Name $ServiceName
    if ($arrService.Status -ne "Running"){
        Start-Service $ServiceName
        FuncMail -To "to-email@domain.com" -From "from-mail@domain.com"  -Subject "Servername : ($ServiceName) service started." -Body "Service $ServiceName started" -smtpServer "relay.mailserver.com"
    }
}
function FuncMail {
    #param($strTo, $strFrom, $strSubject, $strBody, $smtpServer)
    param($To, $From, $Subject, $Body, $smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = $From
    $msg.To.Add($To)
    $msg.Subject = $Subject
    $msg.IsBodyHtml = 1
    $msg.Body = $Body
    $smtp.Send($msg)
}
FuncCheckService -ServiceName "VMware VirtualCenter Server"

Just copy and paste this script, save it as a ps1 file and schedule it to run every x minutes…

PowerShell Script to create functional and ACL Groups

0

Since I had the need to create 2 ACL and 2 functional groups in fixed and predefined OUs for the x-th time and just didn´t want to click my way I started to write this tiny script which created them for me…

import-module ActiveDirectory

$Name1 = "acl_" + $args + "_ro"
$Name2 = "acl_" + $args + "_rw"
$Name3 = "func_" + $args + "_ro"
$Name4 = "func_" + $args + "_rw"

new-adGroup -name $Name1 -GroupScope 1 -Path "<UPN-of-OU>" -GroupCategory 1 -sAMAccountName $Name1
new-adGroup -name $Name2 -GroupScope 1 -Path "<UPN-of-OU>" -GroupCategory 1 -sAMAccountName $Name2
new-adGroup -name $Name3 -GroupScope 1 -Path "<UPN-of-OU>" -GroupCategory 1 -sAMAccountName $Name3
new-adGroup -name $Name4 -GroupScope 1 -Path "<UPN-of-OU>" -GroupCategory 1 -sAMAccountName $Name4

Add-ADGroupMember -Identity $Name1 -Member $Name3
Add-ADGroupMember -Identity $Name2 -Member $Name4

Start and Stop Windows services via PowerShell

0

Since the Windows Update service is deactivated in the VMware View environment we have to ‘activate’ and start the service to update the VM.

Here you find some PowerShell?scripts which are starting and stopping this service and you´ll be able to update your VM.

Start Service:

Clear-Host
$srvName = 'wuauserv'
$servicePrior = Get-Service $srvName
'$srvName is now ' + $servicePrior.status
Set-Service $srvName -startuptype manual
Start-Service $srvName
$serviceAfter = Get-Service $srvName
'$srvName is now ' + $serviceAfter.status

Stop Service:

Clear-Host
$srvName = 'wuauserv'
$servicePrior = Get-Service $srvName
'$srvName is now ' + $servicePrior.status
Stop-Service $srvName
$serviceAfter = Get-Service $srvName
Set-Service $srvName -startuptype disabled
'$srvName is now ' + $serviceAfter.status

Shutdown and Recovery script in PowerShell

0

A few months ago I needed a script to shutdown my VMs and the underlying hosts in case of a power outage in one of our datacenters.

For this reason I´ve created some scripts that are shutting down our VMs or in case of recovery abort this process.

Because of security reasons I´ve also implemented the VICredentialStoreItem. So I didn´t had to put the username and password in clear-text into the scripts.

If you´re more interested in how to implement this Feature you can visit one of the following links:

http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/New-VICredentialStoreItem.html

http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Get-VICredentialStoreItem.html

 

Shutdown script:

# Declaring the variables
$enddate = (Get-Date).tostring("yyyyMMdd")
$filename = 'C:\ShutDown' + $enddate + '.log'
$creds = Get-VICredentialStoreItem -file "C:\credentials.xml"
$scriptwaittime = 3600 #Seconds
$shutdownwaittime = 240 #Seconds
$Cluster = '"<Clustername>"'
$ESXSRV1 = Get-VMhost <ESXHost1>
$ESXSRV2 = Get-VMhost <ESXHost2>

# Main script
Start-Transcript $filename
Write-Host "Waiting for about $scriptwaittime seconds in case the power is back" -ForegroundColor Green Write-Host "and the VMs and ESX Host do not have to shut down" -ForegroundColor Green
Start-Sleep $scriptwaittime

# Importing the VMware PowerCLI Snapin into Powershell
if (((Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) -and ((Get-PSSnapin –registered -Name "VMware.VimAutomation.Core") -ne $null)) {     Write-Host "Adding VMware vSphere PowerCLI" -ForegroundColor Green     Add-PSSnapin -name VMware.VimAutomation.Core     ."C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1" }

# Connecting to vCenter Server
Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password

# Set DRS to manual
Set-Cluster -Cluster $Cluster -DrsMode manual -Confirm:$false

# For each of the VMs on esxsrv1
# - Shutdown the guest cleanly
Foreach ($VM in ($ESXSRV1 | Get-VM | Where-Object {$_.Folder -notmatch “vCenter”})){$VM | Shutdown-VMGuest -Confirm:$false}

# For each of the VMs on esxsrv2
# - Shutdown the guest cleanly
Foreach ($VM in ($ESXSRV2 | Get-VM | Where-Object {$_.Folder -notmatch “vCenter”})){$VM | Shutdown-VMGuest -Confirm:$false}

# Set the amount of time to wait before assuming the remaining powered on guests are stuck
Write-Host "Sleep $shutdownwaittime seconds" -ForegroundColor Green Start-Sleep $Waittime

# Set Hoststate to Maintenance for esxsrv1
Set-VMHost -VMhost $ESXSRV1 -State “Maintenance”

# Shutdown of esxsrv1
$ESXSRV1 | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)} Write-Host "Shutdown $ESXSRV1 Complete" -ForegroundColor Green

# Set Hoststate to Maintenance for esxsrv2
Set-VMHost -VMhost $ESXSRV2 -State “Maintenance”

# Shutdown of esxsrv2
$ESXSRV2 | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)} Write-Host "Shutdown $ESXSRV2 Complete" -ForegroundColor Green
Stop-Transcript

Recovery script:

$enddate = (Get-Date).tostring("yyyyMMdd") $filename = 'C:\Recovery' + $enddate + '.log'
Start-Transcript $filename
Write-Host "Received recovery signal from ups" -ForegroundColor Green
Write-Host "Going to kill Powershell process" -ForegroundColor Green
Stop-Process -processname powershell*
Write-Host "Powershell process was killed" -ForegroundColor Green
Stop-Transcript
Go to Top