Powershell

Timer : Calculate total and iteration elapsed time

<# ============================================================================================== Script permettant d'avoir des timers dans un script. ============================================================================================== #>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Timer
 
$script:startTime = Get-Date
 
function GetElapsedTime([datetime]$starttime) 
{
    $runtime = $(get-date) - $starttime
    $retStr = [string]::format("{0} sec(s)", $runtime.TotalSeconds)
    $retStr
}
 
write-host "Script Started at $script:startTime"
for ($i=1; $i -lt 10; $i++) 
{
    $iterTime = Get-Date ;
    get-process | out-null
    $waitsecs = Get-Random -minimum 1 -maximum 5
    sleep $waitsecs
    $elapsed = GetElapsedTime $iterTime ;
    write-host "   Iteration Time: " $elapsed ;
}
 
write-host "Script Ended at $(get-date)"
$elapsed = GetElapsedTime $script:startTime
write-host "Total Elapsed Time: " $elapsed;
Facebooktwittermail

Check DFS-R

Script permettant de faire un verification de DFS-R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<# =========================================================================================
NAME: check_DFSR.ps1
DATE  : 25/02/2015
 
COMMENT: Vérifie l'état de la réplication du serveur proxy passé en paramètre
INPUT : 
Name of site 
Name of PXY Server
Name of DFS Server
 
============================================================================================
#>
cls
 
$NOM_SITE = read-host "Nom du site (ex. MSD7902) "
$PROXY_CACHE = read-host "Serveur DFS Distant (ex. SWPXY201) "
$SRV_DFS = read-host "Serveur DFS Central (ex. SWDFS1030) "
 
$script:startTime = Get-Date
 
function GetElapsedTime([datetime]$starttime) 
{
    $runtime = $(get-date) - $starttime
    $retStr = [string]::format("{0} sec(s)", $runtime.TotalSeconds)
    $retStr
}
 
 
Write-Host "=========================="
Write-Host "      START DFSRDIAG      "
Write-Host "=========================="
Write-Host "`n"
Write-Host "Nom du site :" $NOM_SITE
Write-Host "Serveur Proxy-Cache :" $PROXY_CACHE
Write-Host "Serveur DFS :" $SRV_DFS
 
write-host "Script Started at $script:startTime"
 
# Diagnostic de la réplication
# Repertoire Collecte
dfsrdiag backlog /rgname:maif.local\dfsmaif\$NOM_SITE /rfname:$NOM_SITE /smem:$PROXY_CACHE /Rmem:$SRV_DFS
 
# Repertoire Publication 
dfsrdiag backlog /rgname:maif.local\dfsmaif\publication /rfname:publication /smem:$SRV_DFS /Rmem:$PROXY_CACHE
 
# Repertoire Scripts
dfsrdiag backlog /rgname:maif.local\dfsmaif\scripts /rfname:scripts /smem:$SRV_DFS /Rmem:$PROXY_CACHE
 
# Repertoire Distribution - Technique & Drivers
dfsrdiag backlog /rgname:maif.local\distribution\technique /rfname:technique /smem:$SRV_DFS /Rmem:$PROXY_CACHE
dfsrdiag backlog /rgname:maif.local\distribution\Drivers /rfname:Drivers /smem:$SRV_DFS /Rmem:$PROXY_CACHE
 
write-host "Script Ended at $(get-date)"
$elapsed = GetElapsedTime $script:startTime
write-host "Total Elapsed Time: " $elapsed;
Facebooktwittermail

Robocopy

Script permettant de faire un robocopy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<# ==============================================================================================
 
NAME: robocopy_DFSR.ps1
DATE  : 01/04/2015
 
COMMENT: Robocopy des fichiers
INPUT :
 
OUTPUT : Logs TXT in D:\Temp\Robocopy\ on $PROXY_CACHE
 ============================================================================================
#>
cls
 
# PARAMETERS
$NOM_SITE = read-host "Nom du site (ex. MSD7902) "
$PROXY_CACHE = read-host "Serveur Proxy-Cache (ex. SWPXY201) "
$SRV_DFS = read-host "Serveur DFS Central (ex. SWDFS1030) "
 
$script:startTime = Get-Date
 
function GetElapsedTime([datetime]$starttime) 
{
    $runtime = $(get-date) - $starttime
    $retStr = [string]::format("{0} sec(s)", $runtime.TotalSeconds)
    $retStr
}
 
Write-Host "=========================="
Write-Host "      START ROBOCOPY      "
Write-Host "=========================="
Write-Host "`n"
 
Write-Host "Nom du site :" $NOM_SITE
Write-Host "Serveur Proxy-Cache :" $PROXY_CACHE
Write-Host "Serveur DFS :" $SRV_DFS
 
write-host "Script Started at $script:startTime"
Write-Host "Robocopy en cours ..."
 
# CMD
#Write-Host "Robocopy $PROXY_CACHE => DFS CENTRAL $SRV_DFS"
#robocopy.exe "D:\Collecte\Commun" "\\$SRV_DFS\d$\Collecte\$NOM_SITE\Commun" /MIR /R:2 /W:10 /LOG+:D:\temp\Robocopy\robocop_$PROXY_CACHE.txt 
#robocopy.exe "D:\Collecte\ProfilItinerant" "\\$SRV_DFS\d$\Collecte\$NOM_SITE\ProfilItinerant" /MIR /R:2 /W:10 /LOG+:D:\temp\Robocopy\robocop_$PROXY_CACHE.txt
#robocopy.exe "D:\Collecte\Technique" "\\$SRV_DFS\d$\Collecte\$NOM_SITE\Technique" /MIR /R:2 /W:10 /LOG+:D:\temp\Robocopy\robocop_$PROXY_CACHE.txt
#robocopy.exe "D:\Collecte\Utilisateurs" "\\$SRV_DFS\d$\Collecte\$NOM_SITE\Utilisateurs" /MIR /R:2 /W:10 /LOG+:D:\temp\Robocopy\robocop_$PROXY_CACHE.txt
 
# Test d'un robocopy SIEGE => PXY 
Write-Host "Robocopy DFS CENTRAL $SRV_DFS => $PROXY_CACHE"
robocopy D:\Collecte\$NOM_SITE\Commun \\$PROXY_CACHE\d$\save_Collecte /XD DfsrPrivate "System Volume Information" $RECYCLE.BIN /DCOPY:T /COPYALL /SEC /MIR /R:0 /W:0 /LOG+:C:\Temp\Collecte_$PROXY_CACHE.txt
 
write-host "Script Ended at $(get-date)"
$elapsed = GetElapsedTime $script:startTime
write-host "Total Elapsed Time: " $elapsed;
Facebooktwittermail

Lister les files d’impression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true)]
    [string[]]$PrintServers = "Nom d'un serveur PXY"
)
 
# Create new Excel workbook
Write-Verbose "$(Get-Date): Script begins!"
Write-Verbose "$(Get-Date): Opening Excel..."
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Name = "Inventaire Imprimantes"
#======================================================
$Sheet.Cells.Item(1,1) = "Print Server"
$Sheet.Cells.Item(1,2) = "Printer Name"
$Sheet.Cells.Item(1,3) = "Location"
$Sheet.Cells.Item(1,4) = "Comment"
$Sheet.Cells.Item(1,5) = "IP Address"
$Sheet.Cells.Item(1,6) = "Driver Name"
$Sheet.Cells.Item(1,7) = "Driver Version"
$Sheet.Cells.Item(1,8) = "Driver"
$Sheet.Cells.Item(1,9) = "Shared"
$Sheet.Cells.Item(1,10) = "Share Name"
#=======================================================
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 36
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
#=======================================================
 
# Get printer information
ForEach ($PrintServer in $PrintServers)
{   Write-Verbose "$(Get-Date): Working on $PrintServer..."
 
    $Printers = Get-WmiObject Win32_Printer -ComputerName $PrintServer
    ForEach ($Printer in $Printers)
    {        
		If ($Printer.Name -notlike "Microsoft XPS*")
        {
			$Valeur = Test-Connection -ComputerName $Printer.Name -BufferSize 16 -Count 2 -ea 0 -quiet
 
			if ($Valeur -eq $True){ 
	        	Write-host "Réponse de l'imprimante :" $Printer.Name
 
	            $Sheet.Cells.Item($intRow, 1) = $PrintServer
	            $Sheet.Cells.Item($intRow, 2) = $Printer.Name
	            $Sheet.Cells.Item($intRow, 3) = $Printer.Location
	            $Sheet.Cells.Item($intRow, 4) = $Printer.Comment
 
				$Sheet.Cells.Item($intRow, 1).Interior.ColorIndex = 43
				$Sheet.Cells.Item($intRow, 2).Interior.ColorIndex = 43
				$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 43
				$Sheet.Cells.Item($intRow, 4).Interior.ColorIndex = 43
 
				$IPList = nslookup $Printer.Name | findstr "[0-9].[0-9].[0-9].[0-9]" 
				$cpt = 0
				foreach ($IP in $IPList)
				{
				  $cpt++
				  if ($cpt -ne 1){
				    $IP_addr = "$IP" | %{ $_.Split(" ")[2] }
				  }
				}
				$Sheet.Cells.Item($intRow, 5) = $IP_addr
				$Sheet.Cells.Item($intRow, 5).Interior.ColorIndex = 43
 
	            ####################       
	            $Drivers = Get-WmiObject Win32_PrinterDriver -Filter "__path like '%$($Printer.DriverName)%'" -ComputerName $Printserver
	            ForEach ($Driver in $Drivers)
	            {   $Drive = $Driver.DriverPath.Substring(0,1)
	                $Sheet.Cells.Item($intRow, 7) = (Get-ItemProperty ($Driver.DriverPath.Replace("$Drive`:","\\$PrintServer\$Drive`$"))).VersionInfo.ProductVersion
	                $Sheet.Cells.Item($intRow,8) = Split-Path $Driver.DriverPath -Leaf
					$Sheet.Cells.Item($intRow, 7).Interior.ColorIndex = 43
					$Sheet.Cells.Item($intRow, 8).Interior.ColorIndex = 43
	            }
	            ####################      
	            $Sheet.Cells.Item($intRow, 6) = $Printer.DriverName
	            $Sheet.Cells.Item($intRow, 9) = $Printer.Shared
	            $Sheet.Cells.Item($intRow, 10) = $Printer.ShareName
				$Sheet.Cells.Item($intRow, 6).Interior.ColorIndex = 43
				$Sheet.Cells.Item($intRow, 9).Interior.ColorIndex = 43
				$Sheet.Cells.Item($intRow, 10).Interior.ColorIndex = 43
	            $intRow ++   
			}else{
				Write-host "Pas de réponse de l'imprimante :" $Printer.Name
 
	            $Sheet.Cells.Item($intRow, 1) = $PrintServer
	            $Sheet.Cells.Item($intRow, 2) = $Printer.Name
	            $Sheet.Cells.Item($intRow, 3) = $Printer.Location
	            $Sheet.Cells.Item($intRow, 4) = $Printer.Comment
 
				$Sheet.Cells.Item($intRow, 1).Interior.ColorIndex = 3
				$Sheet.Cells.Item($intRow, 2).Interior.ColorIndex = 3
				$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
				$Sheet.Cells.Item($intRow, 4).Interior.ColorIndex = 3
 
	            If ($Printer.PortName -notlike "*\*")
	            {   $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
	                ForEach ($Port in $Ports)
	                {
	                    $Sheet.Cells.Item($intRow, 5) = "N/C"					
						$Sheet.Cells.Item($intRow, 5).Interior.ColorIndex = 3
	                }
	            }
 
	            ####################       
	            $Drivers = Get-WmiObject Win32_PrinterDriver -Filter "__path like '%$($Printer.DriverName)%'" -ComputerName $Printserver
	            ForEach ($Driver in $Drivers)
	            {   $Drive = $Driver.DriverPath.Substring(0,1)
	                $Sheet.Cells.Item($intRow, 7) = (Get-ItemProperty ($Driver.DriverPath.Replace("$Drive`:","\\$PrintServer\$Drive`$"))).VersionInfo.ProductVersion
	                $Sheet.Cells.Item($intRow,8) = Split-Path $Driver.DriverPath -Leaf
					$Sheet.Cells.Item($intRow, 7).Interior.ColorIndex = 3
					$Sheet.Cells.Item($intRow, 8).Interior.ColorIndex = 3
	            }
	            ####################      
	            $Sheet.Cells.Item($intRow, 6) = $Printer.DriverName
	            $Sheet.Cells.Item($intRow, 9) = $Printer.Shared
	            $Sheet.Cells.Item($intRow, 10) = $Printer.ShareName
				$Sheet.Cells.Item($intRow, 6).Interior.ColorIndex = 3
				$Sheet.Cells.Item($intRow, 9).Interior.ColorIndex = 3
				$Sheet.Cells.Item($intRow, 10).Interior.ColorIndex = 3
	            $intRow ++   
			}
		}
    }
	$intRow ++ 
    $WorkBook.EntireColumn.AutoFit() | Out-Null
}
 
$intRow ++ 
Write-Verbose "$(Get-Date): Completed!"
Facebooktwittermail

NSLookup – Get IP Address

Script permettant de recuperer l’IP d’une machine à partir d’un NSLOOKUP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
nslookup - Get IP Address
$FullAddressList = nslookup -type=a $ARecord $NameServerQueried | findstr &quot;[0-9].[0-9].[0-9].[0-9]&quot;
 
$counter = 0
 
foreach ($line in $FullAddressList)
 
{
 
$counter = $counter + 1
 
if ($counter -ne 1)
 
{
 
write &quot;$line&quot; | %{ $_.Split(&quot; &quot;)[2] }
 
}
 
}

viaPowerShell v2 and DNS Queries… | Coretek Services.

Facebooktwittermail

Wake on LAN

Je recherchais un script en Powershell pour démarrer les PC à distance. J’ai trouvé celui-ci sur le technet. A voir si on peut lui indiquer une IP plutôt que la MAC.
Sinon, j’ai un autre script dans les cartons qui doit faire ça. A voir prochainement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Script to wake up a computer. 

# v1.0 
# usage: 
#        .\wakeonlan.ps1 -mac 00:1D:92:51:4C:41 
 
param( 
    [string]$mac = 'FF:00:AA:B2:99:F5' #address of the network card (MAC address) 
) 
 
#checks the syntax of MAC address 
if (!($mac -like "*:*:*:*:*:*") -or ($mac -like "*-*-*-*-*-*")){ 
    write-error "mac address not in correct format" 
    break 
} 
 
#build magic package http://en.wikipedia.org/wiki/Wake-on-LAN#Magic_packet  
$string=@($mac.split(":""-") | foreach {$_.insert(0,"0x")}) 
$target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5]) 
# The magic packet is a broadcast frame containing anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal) 
$packet = [byte[]](,0xFF * 102) 
# followed by sixteen repetitions of the target computer's 48-bit MAC address, for a total of 102 bytes. 
6..101 |% { $packet[$_] = $target[($_%6)]} 
 
# .NET framework lib para sockets 
$UDPclient = new-Object System.Net.Sockets.UdpClient 
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000) 
$UDPclient.Send($packet, $packet.Length) | out-null
Facebooktwittermail

Archives