Change DCOM config security settings using Powershell

Looks like you would do it using WMI. Get an instance of: Win32_DCOMApplicationSetting like this: $dcom = Get-WMIObject -Class Win32_DCOMApplicationSetting -Filter ‘Description=”Something”‘ Now you have access to the SetAccessSecurityDescriptor and SetLaunchSecurityDescriptor methods. From: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384905(v=vs.85).aspx DCOM applications DCOM application instances have several security descriptors. Starting with Windows Vista, use methods of the Win32_DCOMApplicationSetting class to get … Read more

.SelectSingleNode in Powershell script using xPath not working on extracting values from web.config file

Your XML file has a namespace: <configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0″> so you need a namespace manager for SelectSingleNode (see section “Remarks”): XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager. Something like this should work: … Read more

List hidden sub-directories and sizes

The -Force argument for Get-ChildItem will cause it to include hidden files and directories. Get-ChildItem -Force | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.Name + “: ” + “{0:N2}” -f ((Get-ChildItem $_ -Recurse -Force | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) + ” MB” }

How to return specific data type from PowerShell function i.e. DataTable

Rather than return use Write-Output -NoEnumerate. For example: function New-DataTable { $datatable = New-Object System.Data.DataTable $null = $datatable.Columns.Add(“x”,[int]) $null = $datatable.Columns.Add(“y”,[int]) $null = $datatable.Rows.Add(@(1,2)) $null = $dataTable.Rows.Add(@(3,4)) Write-Output -NoEnumerate $datatable } New-DataTable | Get-Member Note however that if you just type New-DataTable, it might look like enumberated rows, but Get-Member tells you the actual type … Read more

Using –% in PowerShell

I had this same problem, and there is a workaround for this problem. I recently blogged about this (Use PowerShell variables after escaping parsing in PowerShell v3.0 – 2013-09-17) after taking inspiration from my own answer, Executing external command from PowerShell is not accepting a parameter. The trick is to use –% itself as a … Read more

What security setting is preventing Remote PowerShell 2.0 from accessing UNC paths

To get this to work, you must configure both your local and remote computers. On the remote server, run the following command: Enable-WSManCredSSP -Role server You’ll know things are confgured correctly if you run the Get-WSManCredSSP cmdlet and get the following output: The machine is not configured to allow delegating fresh credentials. This computer is … Read more

Remove last line from file with Powershell

If you already know that the very last thing of the file is a CRLF you want to get rid of (and you know the encoding too) you can go the quick route: $stream = [IO.File]::OpenWrite(‘foo.txt’) $stream.SetLength($stream.Length – 2) $stream.Close() $stream.Dispose() This is an in-place truncation of the file. It works without reading all the … Read more