I need to export script of all my linked server in my environment to a file for each server.
The following command below successfully scripts out all the linked servers on ServerName1
get-childitem | %{$_.script()} >> C:\Users\someuser\Documents\Powershell\OutputFiles\ServerName1.sql
The issue is that I have 35 servers and in order for this command to work I need to change directories of each server.
In other words the command need to be run as follows for each server
PS SQLSERVER:\SQL\ServerName1\DEFAULT\LinkedServers> get-childitem | %{$_.Script()} >> C:\Users\someuser\Documents\Powershell\OutputFiles\ServerName1.sql
How could I loop a command that runs from different paths based on Server Name and names the file same as the server name?