<# Variables.PS1 Demonstrates the use of Variables sansell@rm.com #> $Date = Get-Date -uformat %A $Name = Read-Host Please enter your name
Write-Host Hello $Name, today is $Date <# Variables.PS1 Demonstrates the use of Variables sansell@rm.com
#> $Global:Date = Get-Date -uformat %A $Global:Name = Read-Host Please enter your name Write-Host Hello $Name, today is $Date <# This is the first line comment This is the second line comment #>
<# IllegalFiles.PS1 This script will search a path for file types sansell@rm.com #> Where Gets objects where the property matches { Encloses the filter $_.CommandType The property to be filtered by. Always prefaced by $_. -eq Equals. This could be like (Like) or ne (Does not equal). Other comparison operators are available Cmdlet One of the available property types, the other was Application
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") #Create Form $form = New-Object System.Windows.Forms.Form $form.text = "A.NET Form" $form.size = New-Object System.Drawing.Size(400,130)
#== Instruction Label $InstructionlabelBox = new-object System.Windows.Forms.Label $InstructionlabelBox.Font = new-object System.Drawing.Font("New Times Roman",9,[System.Drawing.FontStyle]::Bold) $InstructionlabelBox.location = new-object System.Drawing.Size(10,10) $InstructionlabelBox.size = new-object System.Drawing.Size(375,20) $InstructionlabelBox.text = "Click the Engage button" $form.controls.add($instructionlabelbox) #== Run Button $RunButton = new-object System.Windows.Forms.Button $RunButton.Location = new-object System.Drawing.Size(10,43) $RunButton.Size = new-object System.Drawing.Size(375,25) $RunButton.Text = "Engage!" $RunButton.Add_Click($Execute) $form.controls.add($runbutton)
#== Close Button $cabutton = new-object System.Windows.Forms.Button $cabutton.location = new-object System.Drawing.Size(300,75) $cabutton.size = new-object System.Drawing.Size(85,25) $cabutton.text = "Close" $cabutton.add_click({$form.close()}) $form.controls.add($cabutton) $Execute =
{ $Message = "This works" $Title = "Error" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } $Execute = { $Date = Get-Date $Message = "$Date" $Title = "The date and time is:" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } <# Form - 1- Message Box.PS1 This script will search a path for file types sansell@rm.com v0.1
#> $Execute = { $Message = "This works" $Title = "A.NET Message Box" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") #Create Form $form = New-Object System.Windows.Forms.Form $form.text = "A.NET Form" $form.size = New-Object System.Drawing.Size(400,130) #== Instruction Label $InstructionlabelBox = new-object System.Windows.Forms.Label $InstructionlabelBox.Font = new-object System.Drawing.Font("New Times Roman",9,[System.Drawing.FontStyle]::Bold) $InstructionlabelBox.Location = new-object System.Drawing.Size(10,10) $InstructionlabelBox.size = new-object System.Drawing.Size(375,20) $InstructionlabelBox.Text = "Click the Engage button" $form.controls.add($instructionlabelbox) #== Run Button $RunButton = new-object System.Windows.Forms.Button $RunButton.Location = new-object System.Drawing.Size(10,43) $RunButton.Size = new-object System.Drawing.Size(375,25) $RunButton.Text = "Engage!" $RunButton.Add_Click($Execute) $form.controls.add($runbutton) #== Close Button $cabutton = new-object System.Windows.Forms.Button $cabutton.location = new-object System.Drawing.Size(300,75) $cabutton.size = new-object System.Drawing.Size(85,25) $cabutton.text = "Close" $cabutton.add_click({$form.close()}) $form.controls.add($cabutton)
#== Credit Label $CreditLabelBox = new-object System.Windows.Forms.Label $CreditLabelBox.Location = new-object System.Drawing.Size(10,78) $CreditLabelBox.size = new-object System.Drawing.Size(260,20) $CreditLabelBox.Text = "Just messing around with forms - sansell@rm.com" $form.controls.add($creditlabelbox) $form.topmost = $true $form.add_shown({$form.activate()}) $form.showdialog()
<# IllegalFiles.PS1 This script will search a path for file types sansell@rm.com v0.1 #> Get-ChildItem C:\Data Recurse Include *.AVI <# IllegalFiles.PS1 This script will search a path for file types sansell@rm.com v0.1 - Hard coded v0.2 - Path and file type variables added #>
$Path = Read-Host "Enter the path you wish to search" $FileType = Read-Host "Enter the file type you want to search for. Example *.txt" Get-ChildItem $Path Recurse Include "$FileType" <# IllegalFiles.PS1 This script will search a path for file types sansell@rm.com v0.1 - Hard coded v0.2 - Path and file type variables added v0.3 - Output sent to log file
#> $Path = Read-Host "Enter the path you wish to search" $FileType = Read-Host "Enter the file type you want to search for. (Example *.txt)" $LogName = Get-Date -uformat %d%m%y_%h%m%s $LogPath = Read-Host "Enter the log path" Get-ChildItem $Path Recurse Include "$FileType" Out-File $LogPath\$logName.log <# IllegalFiles.PS1 This script will search a path for file types sansell@rm.com v0.1 - Hard coded v0.2 - Path and file type variables added v0.3 - Output sent to log file #> $Path = Read-Host "Enter the path you wish to search"
$FileType = Read-Host "Enter the file type you want to search for. (Example *.txt)" $LogName = Get-Date -uformat %d%m%y_%h%m%s $LogPath = Read-Host "Enter the log path" $ServerName = Hostname $Date = Get-date $Date Out-File $LogPath\$LogName.log $ServerName Out-File $LogPath\$LogName.log -Append Get-ChildItem $Path Recurse Include "$FileType" Out-File $LogPath\$LogName.log - Append
# Command to run when the execute button is clicked $Execute =
{ $Path = $txtpath.text $FileType = $txtfiletype.text $LogName = Get-Date -uformat %d%m%y_%h%m%s $LogPath = $txtlogpath.text $ServerName = Hostname $Date = Get-date $Date Out-File $LogPath\$LogName.log $ServerName Out-File $LogPath\$LogName.log -Append Get-ChildItem $Path Recurse Include "$FileType" Select Directory,Name,Length Out- File $LogPath\$LogName.log -Append } [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") #Create Form $form = New-Object System.Windows.Forms.Form $form.text = "IllegalFiles" $form.size = New-Object System.Drawing.Size(400,350)
#== Instruction Label $InstructionlabelBox = new-object System.Windows.Forms.Label $InstructionlabelBox.Font = new-object System.Drawing.Font("New Times Roman",9,[System.Drawing.FontStyle]::Bold) $InstructionlabelBox.Location = new-object System.Drawing.Size(10,10) $InstructionlabelBox.size = new-object System.Drawing.Size(375,40) $InstructionlabelBox.Text = "Enter the Source Path, File Type to search for and the Log Path then click Engage" $form.controls.add($instructionlabelbox) #== Search Path $txtpath = new-object System.Windows.Forms.TextBox $txtpath.location = new-object System.Drawing.Size(110,50) $txtpath.size = new-object System.Drawing.Size(200,30) $form.controls.add($txtpath) $lblpath = new-object System.Windows.Forms.Label $lblpath.location = new-object System.Drawing.Size(10,53) $lblpath.size = new-object System.Drawing.Size(100,20) $lblpath.text = "Search Path:" $form.controls.add($lblpath) #== Run Button $RunButton = new-object System.Windows.Forms.Button $RunButton.Location = new-object System.Drawing.Size(10,143) $RunButton.Size = new-object System.Drawing.Size(375,25) $RunButton.Text = "Engage!" $RunButton.Add_Click($Execute) $form.controls.add($runbutton)
$cabutton = new-object System.Windows.Forms.Button $cabutton.location = new-object System.Drawing.Size(300,175) $cabutton.size = new-object System.Drawing.Size(85,25) $cabutton.text = "Close" $cabutton.add_click({$form.close()}) $form.controls.add($cabutton)
$PathExists = Test-Path $Path If ($PathExists -eq $False) {$Message = "Search path does not exist, please check path!" $Title = "Error" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } else{ $LogPathExists = Test-Path $LogPath If ($LogPathExists -eq $False)
{$Message = "Log path does not exist, please check path!" $Title = "Error" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } } <# IllegalFiles.PS1 This script will search a path for file types sansell@rm.com v0.1 - Hard coded v0.2 - Path and file type variables added v0.3 - Output sent to log file v0.4 - Selections added v1.0 -.NET form added. v1.1 - Basic error reporting added #> # Command to run when the execute button is clicked $Execute = { #Variables $Path = $txtpath.text $FileType = $txtfiletype.text $LogName = Get-Date -uformat %d%m%y_%h%m%s $LogPath = $txtlogpath.text $ServerName = Hostname $Date = Get-date $Date Out-File $LogPath\$LogName.log $ServerName Out-File $LogPath\$LogName.log -Append #Basic error reporting $PathExists = Test-Path $Path If ($PathExists -eq $False) {$Message = "Search path does not exist, please check path!" $Title = "Error" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } else{ $LogPathExists = Test-Path $LogPath If ($LogPathExists -eq $False) {$Message = "Log path does not exist, please check path!" $Title = "Error" [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.MessageBox]::Show("$Message","$title") } } #Command Get-ChildItem $Path Recurse Include "$FileType" Select Directory,Name,Length Out- File $LogPath\$LogName.log -Append } [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") #Create Form $form = New-Object System.Windows.Forms.Form $form.text = "IllegalFiles" $form.size = New-Object System.Drawing.Size(400,230) #== Instruction Label $InstructionlabelBox = new-object System.Windows.Forms.Label $InstructionlabelBox.Font = new-object System.Drawing.Font("New Times Roman",9,[System.Drawing.FontStyle]::Bold) $InstructionlabelBox.Location = new-object System.Drawing.Size(10,10) $InstructionlabelBox.size = new-object System.Drawing.Size(375,40) $InstructionlabelBox.Text = "Enter the Source Path, File Type to search for and the Log Path then click Engage" $form.controls.add($instructionlabelbox) #== Search Path $txtpath = new-object System.Windows.Forms.TextBox $txtpath.location = new-object System.Drawing.Size(110,50) $txtpath.size = new-object System.Drawing.Size(200,30) $form.controls.add($txtpath) $lblpath = new-object System.Windows.Forms.Label $lblpath.location = new-object System.Drawing.Size(10,53) $lblpath.size = new-object System.Drawing.Size(100,20) $lblpath.text = "Search Path:" $form.controls.add($lblpath)
#== Log Path $txtlogpath = new-object System.Windows.Forms.TextBox $txtlogpath.location = new-object System.Drawing.Size(110,80) $txtlogpath.size = new-object System.Drawing.Size(200,30) $form.controls.add($txtlogpath) $lbllogpath = new-object System.Windows.Forms.Label $lbllogpath.location = new-object System.Drawing.Size(10,83) $lbllogpath.size = new-object System.Drawing.Size(100,20) $lbllogpath.text = "Log Path:" $form.controls.add($lbllogpath) #== File Type $txtfiletype = new-object System.Windows.Forms.TextBox $txtfiletype.location = new-object System.Drawing.Size(110,110) $txtfiletype.size = new-object System.Drawing.Size(80,30) $form.controls.add($txtfiletype) $lblfiletype = new-object System.Windows.Forms.Label $lblfiletype.location = new-object System.Drawing.Size(10,113) $lblfiletype.size = new-object System.Drawing.Size(100,20) $lblfiletype.text = "File Type:" $form.controls.add($lblfiletype) $lblfiletype1 = new-object System.Windows.Forms.Label $lblfiletype1.location = new-object System.Drawing.Size(210,113) $lblfiletype1.size = new-object System.Drawing.Size(150,20) $lblfiletype1.text = "Example: *.txt OR *.exe" $form.controls.add($lblfiletype1) #== Run Button $RunButton = new-object System.Windows.Forms.Button $RunButton.Location = new-object System.Drawing.Size(10,143) $RunButton.Size = new-object System.Drawing.Size(375,25) $RunButton.Text = "Engage!" $RunButton.Add_Click($Execute) $form.controls.add($runbutton) #== Close Button $cabutton = new-object System.Windows.Forms.Button $cabutton.location = new-object System.Drawing.Size(300,175) $cabutton.size = new-object System.Drawing.Size(85,25) $cabutton.text = "Close" $cabutton.add_click({$form.close()}) $form.controls.add($cabutton)
#== Credit Label $CreditLabelBox = new-object System.Windows.Forms.Label $CreditLabelBox.Location = new-object System.Drawing.Size(10,178) $CreditLabelBox.size = new-object System.Drawing.Size(260,20) $CreditLabelBox.Text = "IllegalFiles.PS1 v1.1 by sansell@rm.com" $form.controls.add($creditlabelbox) $form.topmost = $true $form.add_shown({$form.activate()}) $form.showdialog()
<# OldComputers.PS1 This script will search for computers that have not logged on for x days sansell@rm.com v1.0 #> #Import the Active Directory Module Import-Module ActiveDirectory #Get the current system time $Date = Get-Date #Get the number of days to subtract from the current system time $Days = Read-Host "Enter the number of days since last logon" #Create the $Time variable $Time = $Date.AddDays(-$Days) #Find all computer with a logon time older than $Time and display in a grid Get-ADComputer -Filter * Get-ADObject -Properties LastLogonTimeStamp where{(([datetime]::fromfiletime($_.lastlogontimestamp) - ([system.datetime]$time)).totaldays) -lt 0} select-object Name,@{Name="Last Logon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} Out-GridView