I got the output as expected by making this change in the code:
{ invoke-sqlcmd -ServerInstance $s -Database $Database -InputFile $FilePath -querytimeout 60000 -User username -Password password | Select-Object -ExpandProperty Column1 | Out-String -Width 4096 | Out-File "c:\temp\dbm\output_$s.txt" ; }
How to exclude PSComputerName and PSSourceJobInstanceId from Powershell output
Amrita Visalam
26
Reputation points
Trying to run this script across multiple database servers and coming up with the following output files:
Script:
#assume SQLPS module is installed (which comes with sql server 2012)
Import-Module sqlps -DisableNameChecking;
set-location c:
#create a workflow to run one script against multiple sql instances
WorkFlow Run-PSQL2 #PSQL means Parallel SQL
{
param(
[Parameter(Mandatory=$true)]
[string[]]$ServerInstance, # string array to hold multiple sql instances
[Parameter(Mandatory=$false)]
[string]$Database,
[Parameter(Mandatory=$true)]
[string]$FilePath # filepath to the t-sql script to be run
)
foreach ($s in $ServerInstance)
{ invoke-sqlcmd -ServerInstance $s -Database $Database -InputFile $FilePath -querytimeout 60000 -User username -Password password | Select-Object "Column1" | Out-String -Width 4096 | Out-File "c:\temp\dbm\output_$s.txt" ; }
} #Run-PSQL2
Run-PSQL2 -ServerInstance 'server01', 'server02', 'server03' -Database master -FilePath 'c:\temp\dbm\a.sql';
Output of one file:
Column1 PSComputerName PSSourceJobInstanceId
------- ------------- ---------------------
exec msdb.dbo.rds_backup_database @source_db_name='Database1', @s3_arn_to_backup_to='arn:aws:s3:::rds-backup-to-az-production/Database1_FULL_Migration.BAK', @overwrite_S3_backup_file=1 localhost bf7dg539-217a-4d0a-9b99-aa7c12167sdf
exec msdb.dbo.rds_backup_database @source_db_name='Database2', @s3_arn_to_backup_to='arn:aws:s3:::rds-backup-to-az-production/Database2_FULL_Migration.BAK', @overwrite_S3_backup_file=1 localhost bf7dg539-217a-4d0a-9b99-aa7c12167sdf
How do I get rid of the PSComputerName and PSSourceJobInstanceId columns?
Windows for business | Windows Server | User experience | PowerShell
8,330 questions
2 answers
Sort by: Most helpful
-
Amrita Visalam 26 Reputation points
2022-09-09T09:45:52.39+00:00 -
SChalakov 10,576 Reputation points MVP Volunteer Moderator
2022-09-08T10:50:48.737+00:00 Hey,
I cannot test this currently, but why just not getting the Column1? Something like that:
$Column1 = (Run-PSQL2 -ServerInstance 'server01', 'server02', 'server03' -Database master -FilePath 'c:\temp\dbm\a.sql').Column1
----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Stoyan Chalakov