I want publish some IT decision here.
This is powershell script for automatically work on storage. Like as storage have some type of devices and domain customers come in and take it for using and after bring back.
Storage worker using this decision can have reports:
- How many devices left on the stock (on screen)
- Who is using any device right now (on screen/printed/save txt or csv file)
- Who used device before (log file)
Script supported first installation and made start icon on Desktop start by double click for easiest work. All data saved on files.
AirWatch.ps1
This is powershell script for automatically work on storage. Like as storage have some type of devices and domain customers come in and take it for using and after bring back.
Storage worker using this decision can have reports:
- How many devices left on the stock (on screen)
- Who is using any device right now (on screen/printed/save txt or csv file)
- Who used device before (log file)
Script supported first installation and made start icon on Desktop start by double click for easiest work. All data saved on files.
#new install
function New_Install(){
$First_Install = Test-Path "C:\airwatch"
if ($First_Install -eq $False){
New-Item -path "C:\airwatch" -name "Devices" -type "directory" #create work folders
New-Item -path "C:\airwatch" -name "Log" -type "directory"
cls
$AppLocation = "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" #create desktop icon
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:USERPROFILE\Desktop\AirWatch.lnk")
$Shortcut.TargetPath = $AppLocation
$Shortcut.Arguments =" -command & $env:USERPROFILE\Documents\airwatch.ps1"
$Shortcut.IconLocation = "mmres.dll,3"
$Shortcut.Description ="AirWatch"
$Shortcut.WorkingDirectory ="%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
$Shortcut.Save()
Write-Host "New
folders created" -foregroundcolor "DarkGreen"
pause 0
Ghange_Font
}else{Write-Host "Not possible create new folders." -foregroundcolor "Red"}
}
#change font on powershell
function Ghange_Font(){
cd 'HKCU:\Control Panel\Desktop'
$val = Get-ItemProperty -Path . -Name "LogPixels"
if($val.LogPixels -ne 96)
{
Write-Host 'Change
to 100% / 96 dpi'
Set-ItemProperty -Path . -Name LogPixels -Value 96
} else {
Write-Host 'Change
to 150% / 144 dpi'
Set-ItemProperty -Path . -Name LogPixels -Value 144
}
#logoff;exit #logoff after change font in
registry - for sure
}
#assign scanned device number for
CustomerID
function Assign_Process($Search_ID){
do {
$CustomerID = Read-Host "Enter
customer ID"
$CustomerID = $CustomerID.Trim()
if ($CustomerID -match '[^a-zA-Z0-9]') {$CustomerID = ""}
if ($CustomerID -eq "e") {exit}
if($CustomerID -eq "")
{
write-host "Please
customer ID" -foregroundcolor "Red"
write-host ""
}
} until ($CustomerID -ne "")
Try {
$Customer_NameAD = Get-ADUser $CustomerID -Properties * | select DisplayName
$Customer_Name = $Customer_NameAD.DisplayName
$FullName = Get-Date -Format "dd-MMM-yyyy HH-mm"
$FileName = $FullName + "+"+ $Customer_Name
New-Item -path "C:\airwatch\Devices\$Search_ID" -name "$FileName.txt" -type "file" -value $FileName
cls
Write-Host ""
Write-Host "--------------------------------------------"
Write-Host "Device - " -nonewline
Write-Host $Search_ID -foregroundcolor "DarkGreen" -nonewline
Write-Host " assigned for: " -nonewline -foregroundcolor "Gray"
Write-Host $Customer_NameAD.DisplayName -foregroundcolor "DarkGreen"
Write-Host "--------------------------------------------"
Write-Host ""
Pause 0
#}
}
Catch
[Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {Write-Host "User does not exist in AD" -foregroundcolor "Red";Pause 0; exit}
}
#rename file after return
function Return_Process($Search_ID)
{
$Device_using = Get-ChildItem "C:\airwatch\Devices\$Search_ID" | Where-Object {$_.Extension -eq ".txt"} | select name
$FileName_for_Rename = $Device_using.Name
$start = ($FileName_for_Rename.IndexOf("+"))+1
$end = $FileName_for_Rename.Length - $start - 4
$end_file = $FileName_for_Rename.Length - 4
$Renamed_File = $FileName_for_Rename.Substring(0,$end_file)
$Confirm = Read-Host "Return
device from "$FileName_for_Rename.substring($start,$end)"? (Y)"
if ($Confirm -eq "y"){
$Log_File = "C:\airwatch\Log\Airwatch.log"
$Return_Data = Get-Date -Format "dd-MMM-yyyy HH-mm"
Rename-Item -path "C:\airwatch\Devices\$Search_ID\$FileName_for_Rename" -newname "$Renamed_File"
Add-Content "C:\airwatch\Devices\$Search_ID\$Renamed_File" " $Search_ID Returned: $Return_Data"
$OutArray = "Used: " + $Renamed_File + "+ Returned: " + $Return_Data
$OutArray | out-file -filepath $Log_File -Append -noClobber
Write-Host "Returned" -foregroundcolor "DarkGreen"
Pause 0
}
}
#make report on screen and save to txt or
csv file
function Reporting (){
$Report = Get-ChildItem "C:\airwatch\Devices" -Recurse -Filter "*.txt"
cls
$Report_File = "C:\airwatch\report.txt"
$Report_Excell = "C:\airwatch\report.csv"
$Output =""
$CrLf = [char]13 + [char]10
$Output += "" + $CrLf
$Output += "-------------------Total Devices: " + $Report.Count + "
-------------------------" + $CrLf + "Data:
"
$Output += Get-Date -Format "dd-MMM-yyyy"
$Output += "" + $CrLf + "Time:
"
$Output += Get-Date -Format "HH-mm"
$Output += "" + $CrLf
$Output += "" + $CrLf
$Output
$Output_Notepad += "Total Devices :" + "`t" + $Report.Count + $CrLf + $CrLf + "Device" + "`t" + "used
by" + "`t" + "from
(time)" + "`t" + "from
(date)" + $CrLf
for ($i=0; $i -le $Report.Count-1; $i++)
{
if ($Report.count -eq 1){$Correction = @();$Correction += $Report, $Report;$Report = $Correction;$i++}
#Write-Host $i #test
output
$start_Customer_Name = ($Report.Name[$i].IndexOf("+"))+1
$end_Customer_Name = $Report.Name[$i].Length - $start_Customer_Name - 4
$Assigned = $Report.Name[$i].Substring($start_Customer_Name,$end_Customer_Name)
$Folder_Name_Lenght = $Report.FullName[$i].Substring(20).IndexOf("\")
$Folder_Name = $Report.FullName[$i].Substring(20,$Folder_Name_Lenght)
$data_used_end = $Report.Name[$i].IndexOf("
")
$time_used = $Report.Name[$i].Substring($data_used_end+1, 5)
$data_used = $Report.Name[$i].Substring(0, $data_used_end)
$Output += "Device - " + $Folder_Name + " used by " + $Assigned + " from " + $time_used[0] + $time_used[1] + ":" + $time_used[3] + $time_used[4] + "/" + $data_used + $CrLf
$Output_Notepad += $Folder_Name + "`t" +$Assigned + "`t" + $time_used[0] + $time_used[1] + ":" + $time_used[3] + $time_used[4] + "`t" + $data_used + $CrLf
Write-Host "Device
- " -nonewline
Write-Host $Folder_Name -foregroundcolor "DarkGreen" -nonewline
Write-Host "
used by " -foregroundcolor "White" -nonewline
Write-Host $Assigned -foregroundcolor "DarkGreen" -nonewline
Write-Host "
from " -foregroundcolor "White" -nonewline
Write-Host $time_used " / " $data_used -foregroundcolor "DarkGreen"
}
Write-Host "--------------------------------------------"
Write-Host ""
Write-Host "If
you want VIEW devices on stock (S)"
$Report = Read-Host "Do
you want VIEW in notepad(N) or VIEW in csv excell format(C) or PRINT (P) this
report?"
switch ($Report){
n {$Output | Out-File $Report_File; Start-Process $Report_File}
c {$Output_Notepad | Out-File $Report_Excell; Start-Process $Report_Excell}
p {out-printer -inputobject $Output}
s {cls
$On_Stock_Devices = @()
$Devices = Get-ChildItem "C:\airwatch\Devices" | ?{$_.psiscontainer} | select name
for ($element = 0; $element -lt $Devices.Count; $element++){
$s = $Devices[$element].Name
$in_use = Get-ChildItem "C:\airwatch\Devices\$s" | Where-Object {$_.Extension -eq ".txt"}
if (!($in_use.count -eq 1)){$On_Stock_Devices += $s
Write-Host "Device
- " -nonewline
Write-Host $s -foregroundcolor "DarkGreen"
}
}
Write-Host ""
Write-Host "Total
Devices: "$On_Stock_Devices.Count
Pause 0
}
default {exit}
}
}
#Main part
do {
$First_Install = Test-Path "C:\airwatch"
if ($First_Install -eq $False){
$Search_ID = Read-Host "First
Install (I); Exit (E)"
if ($Search_ID -eq "E") {exit}
if ($Search_ID -eq "I"){New_Install; Pause 0; Exit}
Exit
}
write-host "Report
(R); Exit (E)"
write-host ""
$Search_ID = Read-Host "Enter
device ID"
$Search_ID = $Search_ID.Trim()
if ($Search_ID -match '[^a-zA-Z0-9]') {$Search_ID = ""}
if($Search_ID -eq "")
{
write-host "Please
Enter ID your device $Search_ID" -foregroundcolor "Red"
write-host ""
}
} until ($Search_ID -ne "")
$new_device = Test-Path "C:\airwatch\Devices\$Search_ID"
if ($new_device -eq $True){
$Devices = Get-ChildItem "C:\airwatch\Devices\$Search_ID" | Where-Object {$_.Extension -eq ".txt"} | select name
switch ($Devices.name.Length -eq 0)
{
$True {Write-Host "on stock" -foregroundcolor "DarkGreen"; Assign_Process $Search_ID} #assign
$False { #return
Write-Host "used
by customer" -foregroundcolor "DarkGreen"
Return_Process $Search_ID}
}
}else{
if ($Search_ID -eq "R"){Reporting; Exit}
if ($Search_ID -eq "E") {exit}
if ($Search_ID -eq "Install"){New_Install; Pause 0; Exit}
#if device number have only numbers - uncomment
#$Search_ID = [int]$Search_ID
Write-Host "Device $Search_ID didn't
found" -foregroundcolor "Red"
$New_Device = Read-Host "Do
you want ENTER new device? New device name $Search_ID "
switch ($New_Device){
y {New-Item -path "C:\airwatch\Devices" -name $Search_ID -type "directory";cls;Write-Host "Device
" $Search_ID "created" -foregroundcolor "DarkGreen";pause 0}
default {}
}
}
No comments:
Post a Comment