PowerShell
Velkommen til PowerHell.dk - Min wiki side om PowerShell.
PowerShell er Microsoft's mere eller mindre efter ligning af UNIX's stærke kommandoer. PowerShell er bygget ind i det fleste server programmer fra MS idag, og kan der med administreres med PowerShell.
Indholdsfortegnelse
Diverse
MessageBox:
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) [Windows.Forms.MessageBox]::Show("Tekst.", "Overskrift!", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
Disk plads
$Cdrev = Get-WmiObject Win32_logicaldisk | where {$_.DeviceId -eq "C:" } $FreeSpace = $Cdrev.FreeSpace $FreeSpaceMB = $FreeSpace/1000000 $FreeSpaceText = "{0:N2}" -f $FreeSpaceMB $1GB = 1070000000 $credentials = new-object Management.Automation.PSCredential “user”, (“pass” | ConvertTo-SecureString -AsPlainText -Force) $recipients = "user1@domain.dk","user2@domain.dk" if($Cdrev.FreeSpace -lt $1GB) { Send-MailMessage -SmtpServer smtpServer -Credential $credentials -To $recipients -From DiskAlerts@domain.dk -Subject "Disk C - under 1 GB!" -Body "C-drev er under 1GB paa SQL serveren - Ledigplads: $FreeSpaceText MB" -Priority High }
Active Directory
import-module ActiveDirectory $OU_Name = 'IT' $StartPassword = '123456Pw' New-ADOrganizationalUnit -Name $OU_Name -Path "OU=Users,DC=domain,DC=local" New-ADGroup -Name "$OU_Name-Users" -Path "OU=$OU_Name,OU=Users,DC=domain,DC=local" -groupScope global New-ADOrganizationalUnit -Name Users -Path "OU=$OU_Name,OU=Users,DC=domain,DC=local" import-csv .\"$OU_Name-Brugere.csv" | %{ new-aduser -Name $_.DisplayName -DisplayName $_.DisplayName -GivenName $_.GivenName -SamAccountName $_.SamAccountName -UserPrincipalName $_.UserPrincipalName -Surname $_.Surname -Path "OU=Users,OU=$OU_Name,OU=Users,DC=domain,DC=local" -CannotChangePassword $false -ChangePasswordAtLogon $false -ProfilePath "\\server\profiles\$($_.SamAccountName)" -HomeDrive 'Z' -HomeDirectory "\\server\user-homes\$($_.SamAccountName)"; Set-ADAccountPassword -identity $_.SamAccountName -NewPassword (ConvertTo-SecureString -AsPlainText $StartPassword -Force) -Reset; Enable-ADAccount -identity $_.SamAccountName Add-ADGroupMember -Identity "$OU_Name-Users" -Member $_.SamAccountName }
CSV fil:
SamAccountName,UserPrincipalName,GivenName,Surname,DisplayName
Eksportere medlemmer af grupper fra en OU:
# Henter grupper fra Groups, henter medlemmer af dem, eksportere til csv filer, og sender e-mail import-module ActiveDirectory $Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=Groups,DC=domain,DC=local" $files = @() Foreach($G In $Groups) { $G_Name = $G.Name $alist = "Name`tUserPrincipalName`tAccountName`tDescription`tLastLogonTimestamp`tCompany`twhenCreated`tAcctEnabled`n" $userlist = Get-ADGroupMember $G_Name | Get-ADUser -properties * | Select-Object -Property Name,UserPrincipalName,SamAccountName,Description,lastLogonTimestamp,Company,whenCreated,Enabled $userlist | ForEach-Object { $arec = $_.Name,$_.UserPrincipalName,$_.SamAccountName,$_.Description,[datetime]::FromFileTime($_.lastLogonTimestamp).ToString('HH:mm d MMMM yyyy'),$_.Company,$_.whenCreated,$_.Enabled $aline = ($arec -join "`t") + "`t" + "`n" $alist += $aline } $alist | Out-File C:\temp\$G_Name.csv $files = $files + "C:\temp\$G_Name.csv" } $credentials = new-object Management.Automation.PSCredential “user”, (“pass” | ConvertTo-SecureString -AsPlainText -Force) Send-MailMessage -SmtpServer smtpserver -Credential $credentials -To "modtager" -From "afsender" -Subject "Bruger oversigt - $date" -Body "Hermed bruger lister fra serveren." -Attachments $files
Sætte password for flere brugere
Og sætte password til aldrig at udløbe.
$adusers = Get-ADUser -Filter {sAMAccountName -like "*company"} $StartPassword = "P@ssword" foreach ($item in $adusers) {Set-ADAccountPassword -identity $item -NewPassword (ConvertTo-SecureString -AsPlainText $StartPassword -Force) -Reset;} foreach ($item in $adusers) {Set-ADUser -Identity $item -PasswordNeverExpires $true}
Exchange
List brugere efter hvor meget data de bruger:
Get-MailboxStatistics | where {$_.ObjectClass –eq “Mailbox”} | Sort-Object TotalItemSize –Descending | ft DisplayName,@{expression={$_.TotalItemSize.Value.ToMB()}},TotalItemSize,ItemCount
IIS
http://learn.iis.net/page.aspx/447/managing-iis-with-the-iis-70-powershell-snap-in/
New-WebVirtualDirectory http://technet.microsoft.com/en-us/library/ee790582.aspx
IIS:\>New-WebVirtualDirectory -Site "Default Web Site" -Name ContosoVDir -PhysicalPath c:\inetpub\contoso
New-Website http://technet.microsoft.com/en-us/library/ee790605.aspx
IIS:\>New-WebSite -Name TestSite -Port 80 -HostHeader TestSite -PhysicalPath "$env:systemdrive\inetpub\testsite"
Fundet på sexchange:
$objIIS = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root") $children = $objIIS.psbase.children $vDir = $children.add("NewFolder",$objIIS.psbase.SchemaClassName) $vDir.psbase.CommitChanges() $vDir.Path = "C:\Documents and Settings\blah\Desktop\new" $vDir.defaultdoc = "Default.htm" $vDir.psbase.CommitChanges()
Indstillinger
Sæt standard printer:
$Printer = Get-WmiObject win32_printer | where {$_.name -match "HP-Printer"} $Printer.SetDefaultPrinter()
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"
Fra: http://www.amikkelsen.com/?p=472
Tilpasset til at tjekke flere services, her Microsoft Dynamics Nav services:
$NavServices = Get-Service | Where-Object {$_.name -like "MicrosoftDynamicsNavServer*"} function FuncCheckService{ param($ServiceName) $arrService = Get-Service -Name $ServiceName if ($arrService.Status -ne "Running"){ Write-Host $_.Name "kører ikke, starter den..." Start-Service $ServiceName Write-Host $_.Name "startet." } else { Write-Host "..." $_.Name "kører som den skal.`n" } } $NavServices | ForEach-Object { Write-Host "Tjekker om" $_.Name "kører..." FuncCheckService -ServiceName $_.Name }
$NavServicesNotRunning = Get-WmiObject Win32_Service | where{$_.Name -like 'MicrosoftDynamicsNavServer*' -and $_.StartMode -eq "Auto" -and $_.State -ne "Running"} | Select-Object Name $NavServicesNotRunning | ForEach-Object {Start-Service $_.Name}
Sende mail med SMTP auth
$credentials = new-object Management.Automation.PSCredential “user”, (“pass” | ConvertTo-SecureString -AsPlainText -Force) Send-MailMessage -SmtpServer server -Credential $credentials -To modtager@domain.dk -From fra@domain.dk -Subject "Emne" -Body "Tekst" -Attachments $files
Tips
http://myitforum.com/cs2/blogs/rcrumbaker/archive/2006/05/10/20079.aspx
ADSI Scripting with Windows PowerShell
http://larsjoergensen.net/tag/powershell