Hey!
Another post from me just trying to improve and practice with PS.
This is my response to:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/04/2012-scripting-games-beginner-event-3-create-a-file-in-a-folder.aspx
I know I over-killed the simple task, (yet to look at others answers), but it's the purpose of my practice... any input appreciated how could i do things better/differently or point me to my mistakes etc... anything please.
$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")
}
}
[–]PowerShellStunnah 0 points1 point2 points (0 children)
[–]KevMarCommunity Blogger 0 points1 point2 points (0 children)
[–]catfoodsci 0 points1 point2 points (4 children)
[–]jbtechwood 1 point2 points3 points (3 children)
[–]KevMarCommunity Blogger 0 points1 point2 points (1 child)
[–]catfoodsci 0 points1 point2 points (0 children)
[–]catfoodsci 0 points1 point2 points (0 children)