PowerShell To Set Folder Permissions

Specifying inheritance in the FileSystemAccessRule() constructor fixes this, as demonstrated by the modified code below (notice the two new constuctor parameters inserted between “FullControl” and “Allow”). $Acl = Get-Acl “\\R9N2WRN\Share” $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule(“user”, “FullControl”, “ContainerInherit,ObjectInherit”, “None”, “Allow”) $Acl.SetAccessRule($Ar) Set-Acl “\\R9N2WRN\Share” $Acl According to this topic “when you create a FileSystemAccessRule the way you have, … Read more

How to get Powershell Invoke-Restmethod to return body of http 500 code response

The other answer does get you the response, but you need an additional step to get the actual body of the response, not just the headers. Here is a snippet: try { $result = Invoke-WebRequest … } catch { $result = $_.Exception.Response.GetResponseStream() $reader = New-Object System.IO.StreamReader($result) $reader.BaseStream.Position = 0 $reader.DiscardBufferedData() $responseBody = $reader.ReadToEnd(); }

try, catch doesent seem to work

In PowerShell there are terminating and non-terminating errors. The former terminate script execution (if not caught) and can be caught by try..catch, the latter don’t terminate script execution (so there’s nothing to catch). The error you’re receiving is a non-terminating one, so you need to make it a terminating error by appending -ErrorAction Stop to … Read more

How to load assemblies in PowerShell?

LoadWithPartialName has been deprecated. The recommended solution for PowerShell V3 is to use the Add-Type cmdlet e.g.: Add-Type -Path ‘C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll’ There are multiple different versions and you may want to pick a particular version. 🙂