Tu sei qui: Home / Guide / Scripts / PS: Windows PowerShell

PS: Windows PowerShell

es.: powershell -ExecutionPolicy Unrestricted -file \\Srv1\sysvol\consinfo.local\scripts\test.ps1

Creazione collegamento sul desktop utente

$wshshell = New-Object -ComObject WScript.Shell
$lnk = $wshshell.CreateShortcut("$home\Desktop\NameShortcut.lnk")
$lnk.TargetPath = "C:\CONSINFO.IT\prg"
$lnk.Save()

Collegamento con argomenti

es. %programfiles%\Internet Explorer\iexplore.exe http://www.consinfo.it

$lnk.TargetPath = "%programfiles%\Internet Explorer\iexplore.exe"
$lnk.Arguments = "http://www.consinfo.it"

Collegamento con impostato "Esegui come amministratore"

Rif.: http://stackoverflow.com/questions/28997799/how-to-create-a-run-as-administrator-shortcut-using-powershell

 $lnk = $wshshell.CreateShortcut("$home\Desktop\Test.lnk")
 $lnk.TargetPath = "powershell"
 $lnk.Arguments = "-ExecutionPolicy RemoteSigned -File C:\test.ps1"
 $lnk.Save()

 $bytes = [System.IO.File]::ReadAllBytes("$home\Desktop\Test.lnk")
 $bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
 [System.IO.File]::WriteAllBytes("$home\Desktop\Test.lnk", $bytes)

Condizione if

  if ($x -eq 1) {Write-Host "111"}  elseif ($x -eq 2) {Write-Host "222222"} else  {Write-Host "errrrr"}

verifica la versione del sistema operativo di un determinato computer (input name)

$strComputer = Read-Host "Printer Report – Enter Computer Name"
$OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" -ComputerName $strComputer
# if statement to run code for Windows XP
if (($OS.Version -eq "5.1.2600"))
{
 write-host "Computer Name: " $strComputer
 write-host "OS Version: Windows XP"
}
# if statement to run code for Windows 7
elseif (($OS.Version -eq "6.1.7600") -or ($OS.Version -eq "6.1.7601"))
{
 write-host "Computer Name: " $strComputer
 if ($OS.Version -eq "6.1.7600") {write-host "OS Version: Windows 7 (no SP)"}
 elseif ($OS.Version -eq "6.1.7601") {write-host "OS Version: Windows 7 SP1"}
}
# if OS not identified
else {write-host "The OS for: $strComputer is not supported."}
write-host "–END OF REPORT–"

Verifica se sistema 32 o 64 bit

if ((Get-WmiObject -Class Win32_OperatingSystem -ea 0).OSArchitecture -eq '64 bit') 
{ #64bit# }
else
{ #32bit# }

Verifica se esiste un file o una cartella

$FileExists = Test-Path "C:\CONSINFO.IT"
If ($FileExists -eq $True) {
 Write-Host "Esiste!"
}

Creare un file o una directory (ref. link)

New-Item C:\CONSINFO.IT\verL0521 -type file

esempio script logon

$PathCI = "C:\CONSINFO.IT"
$FileExists = Test-Path $PathCI
If ($FileExists -eq $False) {
 MD $PathCI
}
$PathCI = "C:\CONSINFO.IT\verL0521"
$FileExists = Test-Path $PathCI
If ($FileExists -eq $False) {
 NET USE G: \\vsrv2008r2\grp
 NET USE S: \\vsrv2008r2\Scambio
 $wshshell = New-Object -ComObject WScript.Shell
 $lnk = $wshshell.CreateShortcut("$home\Desktop\Gruppi.lnk")
 $lnk.TargetPath = "G:"
 $lnk.Save()
 $lnk = $wshshell.CreateShortcut("$home\Desktop\Scambio.lnk")
 $lnk.TargetPath = "S:"
 $lnk.Save()
}
New-Item C:\CONSINFO.IT\verL0521 -type file
Read-Host "Premi [Invio] per continuare"

variabile date e username

get-date >> \\Srv2008\scambio\VM\LOG.LOG
[Environment]::UserName >> \\Srv2008\scambio\VM\LOG.LOG

_

 

Link: PowerShell per WinXp