I am looking to refine my error handling a bit, and hoping for some best practice advice. What I have now, in this Network Location code, is a big Try wrapper on everything. This is working so far, but I wonder if a more refined approach is to separately Try each situation that could fail?
Also, is it common in a case like this to wrap the whole thing in a Transaction? I imagine a failure after creating the desktop.ini would leave behind useless detritus, which isn't good.
FWIW, I am using this as an exercise in general good programming practice, not "quick and dirty scripting", so I am making it more complicated than most PowerShell based stuff, I know. But with the goal of a totally bomb proof program, that just happens to be in PowerShell, what is best practice here?
try { # Create the ini file $DesktopIniContent = ( '[.ShellClassInfo]', 'CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}', 'Flags=2', 'ConfirmFileOp=1' ) -join "`r`n" $DesktopIniContent | Out-File -FilePath:"$NethoodPath\$Name\Desktop.ini" # Create the shortcut file $Link = (New-Object -ComObject:WScript.Shell -ErrorAction:Stop).Createshortcut("$NethoodPath\$Name\target.lnk") $Link.TargetPath = $Path $Link.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 85" $Link.Description = $Path $Link.WorkingDirectory = $Path $Link.Save() # Set attributes on the files & folders Set-ItemProperty "$NethoodPath\$Name\Desktop.ini" -Name:Attributes -Value:([IO.FileAttributes]::System -bxor [IO.FileAttributes]::Hidden) Set-ItemProperty "$NethoodPath\$Name" -Name:Attributes -Value:([IO.FileAttributes]::ReadOnly) }