Chapters/powershell-workflows-primer/solution.ps1

#Requires -version 5.0

<#
- Create a local user account called ITApp with a default password.
- Create a folder called C:\ITApp and share it as ITApp giving Everyone ReadAccess
  and the ITApp user full control.
- Under C:\ITApp create folders Test_1 to Test_10
- Set the Remote Registry service to auto start
- Log each step to a text file called C:\ITAppWF.txt
#>


Workflow ITAppSetup {

Param(
[Parameter(Mandatory)]
[string]$Password,
[string]$Log = "c:\ITAppWF.txt"
)

Set-Content -Value "[$((Get-Date).timeofDay)] Starting Setup" -Path $log

Add-Content -Value "[$((Get-Date).timeofDay)] Configuring Remote Registry service" -Path $log
Set-Service -Name RemoteRegistry -StartupType Automatic

Sequence {
    Add-Content -Value "[$((Get-Date).timeofDay)] Creating local user" -Path $log
    net user ITApp $Password /add
}

Sequence {
    Add-Content -Value "[$((Get-Date).timeofDay)] Testing for C:\ITApp folder" -Path $log
    if (Test-Path -Path "C:\ITApp") {
        Add-Content -Value "[$((Get-Date).timeofDay)] Folder already exists." -Path $log
        $folder = Get-Item -Path "C:\ITApp"
    }
    else {
        Add-Content -Value "[$((Get-Date).timeofDay)] Creating C:\ITApp folder" -Path $log
        $folder = New-Item -Path C:\ -Name ITApp -ItemType Directory
        Add-Content -Value "[$((Get-Date).timeofDay)] Created $($folder.fullname)" -Path $log
    }

    Add-Content -Value "[$((Get-Date).timeofDay)] Testing for ITApp share" -Path $log
    if (Get-SmbShare ITApp -ErrorAction SilentlyContinue) {
        Add-Content -Value "[$((Get-Date).timeofDay)] File share already exists" -Path $log
    }
    else {
      Add-Content -Value "[$((Get-Date).timeofDay)] Creating file share" -Path $log
      New-SmbShare -Name ITApp -Path $folder.FullName -Description "ITApp data" -FullAccess "$($env:computername)\ITApp" -ReadAccess Everyone
    }

    Add-Content -Value "[$((Get-Date).timeofDay)] Creating subfolders" -Path $log
    foreach -parallel ($i in (1..10)) {
        $path = Join-Path -Path $folder.FullName -ChildPath "Test_$i"
        #add a random offset to avoid contention for the log file
        $offset = Get-Random -Minimum 500 -Maximum 2000
        Start-Sleep -Milliseconds $offset
        Add-Content -Value "[$((Get-Date).timeofDay)] Creating $path" -Path $log
        $out = New-Item -Path $folder.FullName -Name "Test_$i" -ItemType Directory 
    }
}
 Add-Content -Value "[$((Get-Date).timeofDay)] Setup complete" -Path $log

} #close workflow

<#
#Undo
net user itapp /del
remove-smbshare ITApp -force
del c:\itapp -recurse
del c:\itappwf.txt
#>
ntent -Value "[$((Get-Date).timeofDay)] Created $($folder.fullname)" -Path $log

    }



    Add-Content -Value "[$((Get-Date).timeofDay)] Testing for ITApp share" -Path $log

    if (Get-SmbShare ITApp -ErrorAction SilentlyContinue) {

        Add-Content -Value "[$((Get-Date).timeofDay)] File share already exists" -Path $log

    }

    else {

      Add-Content -Value "[$((Get-Date).timeofDay)] Creating file share" -Path $log

      New-SmbShare -Name ITApp -Path $folder.FullName -Description "ITApp data" -FullAccess "$($env:computername)\ITApp" -ReadAccess Everyone

    }



    Add-Content -Value "[$((Get-Date).timeofDay)] Creating subfolders" -Path $log

    foreach -parallel ($i in (1..10)) {

        $path = Join-Path -Path $folder.FullName -ChildPath "Test_$i"

        #add a random offset to avoid contention for the log file

        $offset = Get-Random -Minimum 500 -Maximum 2000

        Start-Sleep -Milliseconds $offset

        Add-Content -Value "[$((Get-Date).timeofDay)] Creating $path" -Path $log

        $out = New-Item -Path $folder.FullName -Name "Test_$i" -ItemType Directory 

    }

}

 Add-Content -Value "[$((Get-Date).timeofDay)] Setup complete" -Path $log



} #close workflow



<#

#Undo

net user itapp /del

remove-smbshare ITApp -force

del c:\itapp -recurse

del c:\itappwf.txt

#>