Using .NET library from PowerShell

I have a PowerShell snap-in for manipulating a database. The snap-in itself is just a wrapper for the main library implemented in a separate .NET DLL all written in c#. I have registered both the snap-in and the implementation DLLs with the GAC using installutil.exe Whilst the snap-in works fine in PowerShell, I need to … Read more

Setting private key permissions with PowerShell

I have a PowerShell script that installs pfx certificate into the LocalMachine certificate store. The function looks like this: function Add-Certificate { param ( [Parameter(Position=1, Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$pfxPath, [Parameter(Position=2, Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$pfxPassword ) Write-Host “Installing certificate” -ForegroundColor Yellow try { $pfxcert = new-object $pfxcert.Import($pfxPath, $pfxPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]”PersistKeySet”) $store = new-object -argumentlist “MY”, LocalMachine $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]”ReadWrite”); … Read more

Use Azure Powershell to execute a .sql file

I’ve seen examples of running a SQL command from Azure Powershell e.g. $connectionString = “Data Source=MyDataSource;Initial Catalog=MyDB;User ID=user1;Password=pass1;Connection Timeout=90” $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString) $query = “CREATE TABLE….” $command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection) $connection.Open() $command.ExecuteNonQuery() $connection.Close() Is there a way to replace the $query with a reference to an external file that contains an … Read more

Validate file path parameter

param ([ValidateScript({ Test-Path -Path $_ -PathType Leaf })][string]$filePath) If I declare a parameter like this, will $filePath evaluate to false if it’s an invalid path? Is the point of this to do something like if($filePath) { /* do stuff… */ } or will an exception be thrown? Answer You should use the ValidateScript attribute if … Read more

Pass Powershell parameters within Task Scheduler

function ServiceRestart { Param ( $ErrorLog, $Services, $MaxSize ) $Time = Get-Date -Format ‘yyyy:MM:dd HH:mm:ss’ $Result = (Get-Item $ErrorLog).length if($Result -gt $MaxSize) { Clear-Content $ErrorLog } Try { Foreach($Service in $Services) { Restart-Service -DisplayName $Service -ErrorAction Stop } } Catch { “ERROR: $Service could not be restarted $Time” | Add-Content $ErrorLog } } ServiceRestart -ErrorLog … Read more

VS Code terminal colors are screwy

I’m using the default theme. The terminal’s colors are impossible to read, make no sense, and are inconsistent. I’ve tried switching themes but they’re terrible in all the themes. Here’s what it looks like in the default. Are you able to read my yellow input against the gray background? And why isn’t there a black … Read more

powershell xml xpath

In Powershell, suppose I have the following xml: <Users> <User Name=”Foo”> <Friends> <Friend Name=”Bar”/> </Friends> </User> <User Name=”Foo2″ /> <User Name=”Foo3″> <Friends> <Friend Name=”Bar”/> </Friends> </User> </Users> How can I get all the users that have a “Bar” as a friend? (In this example it would be Foo,Foo3). Should I use xpath? Thanks! Answer I … Read more

How do I disable UAC using Windows PowerShell?

How do I disable UAC using a PowerShell script? I can do this manually via the registry using the adding the following registry entry Key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA Value: 0 Type: DWORD The script should account for the possibility that this key is already present and set incorrectly. Answer New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\policies\system -Name EnableLUA -PropertyType DWord -Value … Read more