Forskel mellem versioner af "PowerShell"
Freesoft (diskussion | bidrag) (→Sæt standard printer baseret på gruppemedlemskab) |
Freesoft (diskussion | bidrag) m |
||
Linje 1: | Linje 1: | ||
− | |||
− | |||
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. | 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. | ||
Nuværende version fra 29. maj 2024, 18:18
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 = "[email protected]","[email protected]" if($Cdrev.FreeSpace -lt $1GB) { Send-MailMessage -SmtpServer smtpServer -Credential $credentials -To $recipients -From [email protected] -Subject "Disk C - under 1 GB!" -Body "C-drev er under 1GB paa SQL serveren - Ledigplads: $FreeSpaceText MB" -Priority High }
Cognitive OCR API
Finde bestemt tekst element i et billede
$url = "Billede URL" #Billede URL $output = "C:\ps\Billede.png" Invoke-WebRequest -Uri $url -OutFile $output #Henter billedet #Udlæser tiden fra billedet via Cognitive OCR API $APIURL = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr" $APIKEY = "KEY" #https://azure.microsoft.com/da-dk/try/cognitive-services/ $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Ocp-Apim-Subscription-Key", $APIKEY) $imgurl = @{url=$url} $json = $imgurl | ConvertTo-Json $response = Invoke-RestMethod $APIURL -Method POST -Body $json -ContentType 'application/json' -Headers $headers $jsonresponse = $response | ConvertTo-Json -Depth 6 $boundingBoxes = ($jsonresponse | ConvertFrom-json) | select -Expand regions | select -Expand lines | select -Expand words $time = $boundingBoxes.text #Dette skulle være hvor teksten er gemt i billedet
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
Liste password udløb ud fra AD gruppe kun brugere sorteret:
Get-ADGroupMember -identity "AD-Users" -recursive | Where objectClass -eq "user" | get-aduser -properties passwordlastset | Sort-Object -Property passwordlastset | ft Name, passwordlastset
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}
Sæt standard printer baseret på gruppemedlemskab
# Få brugerens AD-gruppemedlemskab $ADGroups = (Get-ADUser -Identity $env:USERNAME -Properties MemberOf).MemberOf # Loop gennem hver AD-gruppe og tjek om den starter med "StdPrinter-" foreach ($ADGroup in $ADGroups) { if ($ADGroup -like "CN=StdPrinter-*") { $PrinterName = $ADGroup -replace "CN=StdPrinter-", "" -replace ",OU=Office,DC=domain,DC=dk","" # Find printeren med det angivne navn $Printer = Get-WmiObject win32_printer | where {$_.name -match "$PrinterName"} # Indstil standardprinteren til den fundne printer $Printer.SetDefaultPrinter() # Stop loopet, hvis en passende AD-gruppe er fundet break } }
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 "[email protected]" -From "[email protected]" -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 [email protected] -From [email protected] -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