Skip to content

Instantly share code, notes, and snippets.

@norbinsh
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norbinsh/4473f5ef7c4474914092 to your computer and use it in GitHub Desktop.
Save norbinsh/4473f5ef7c4474914092 to your computer and use it in GitHub Desktop.
practice...
$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'
$WarningPreference = 'Continue'
$Root = "C:\"
$Folder = "C:\2012SG\"
$NestedFolder = "C:\2012SG\event3\"
$FullPath = "C:\2012SG\event3\process3.txt"
function Get-MyProcess
{
Begin
{
$TestFolder = ($Root+"TestFolder"+(Get-Random -Minimum 1 -Maximum 1000))
Write-Debug ('Checking if user "{2}\{1}" has permissions to create a folder under the root drive "{0}"' -f $Root, $env:USERNAME, $env:COMPUTERNAME)
Start-Sleep -s 3
$RootPermission = New-Item -Path $TestFolder -ItemType Directory -Force | Out-null
if ($RootPermission = $false)
{
return Write-Warning ('User "{2}\{1}" DOES NOT have permissions to create a folder under the root drive "{0}"' -f $Root, $env:USERNAME, $env:COMPUTERNAME)
}
else
{
Write-Verbose ('User "{2}\{1}" has permissions to create a folder under the root drive "{0}"' -f $Root, $env:USERNAME, $env:COMPUTERNAME)
}
Write-Debug ('Deleting the TestFolder we just created "{0}"' -f $TestFolder)
Start-Sleep -s 3
$RootFolders = Get-ChildItem $Root -Directory
foreach ($ToDeleteFolder in $RootFolders){
$Timecreated = ((Get-date) - $ToDeleteFolder.CreationTime).Minute
if ($Timecreated -lt 2 -and $ToDeleteFolder.FullName -match "TestFolder*")
{$ToDeleteFolder.Delete()}}
Write-Verbose ('TestFolder "{0}" successfully deleted' -f $TestFolder)
Write-Debug ('Checking if file "{0}" exists' -f $FullPath)
Start-Sleep -s 3
if (Test-Path -Path ($FullPath))
{
Write-Verbose ('"{0}" exists, continuing to Process' -f $FullPath)
}
else
{
Write-Warning ('"{0}" DOES NOT exist,' -f $FullPath)
Write-Debug ('Creating subfolders "{0}"' -f $NestedFolder)
New-Item -Path $NestedFolder -ItemType Directory -Force | Out-Null
New-Item -Path $FullPath -ItemType File -Force | Out-Null
Write-Verbose ('Successfully created "{0}"' -f $FullPath)
}
}
Process
{
Write-Debug ('Getting process list')
Start-Sleep -s 3
Get-Process | Format-table -AutoSize -Wrap -Property Name, Id | Out-File -FilePath $FullPath -Force
Write-Verbose ('Successfully exported process list into the file "{0}"' -f $FullPath)
}
End
{
Start-Sleep -s 3
Write-Verbose ("I think we are done, peace out")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment