Using variables in sqlps - SQL PowerShell

Viewed 122

Is there a way to replace parts of these commands with local variables? When I've tried to do so, it goes out of scope, so instead of running Get-ChildItem on the SQL Server's directory, it does so on the local folder where the command is run from, e.g C:\temp

sqlps -NoLogo -Command { Get-ChildItem -Path SQLSERVER:\SQL\DATABASESERVER_1\DEFAULT\AvailabilityGroups\MyAvailabilityGroup_1\AvailabilityDatabases\ }

I want to replace \DATABASESERVER_1\ with $MyServerNameVariable .

Also, in this part

sqlps -NoLogo -Command { Get-ChildItem -Path SQLSERVER:\SQL\DATABASESERVER_2\"MYINSTANCENAME,25001"\MyAvailabilityGroup_2\MyAvailabilityGroup_2\AvailabilityDatabases\ }

Is there another way to write the instance name of the SQL Server, when you actually want the port number in the string? I know that you normally when configuring a connectionstring, either use only an instance name (SERVER_1\INSTANCE_NAME), OR a FQDN (SERVER_1.DOMAIN.COM,25001) with port number, but in this case it is necessary, since we're using several Availability Groups on the same physical server, and need to separate them.

0 Answers
Related