Hi All.
I have a PS script which runs on an Script server and targets remote machines. Everything works fine, however the issue i am having is that I cannot get the output from my script back. I have tried several ways to do this, the closest thing I have so far is to use the getXML method, it dumps out a lot of XML but all I need is just the filename which my powershell script returns.
I would be grateful if anyone can explain to me how to do this correctly, I will need to do this a lot in the course of my work, sometimes my Ps file will be expected to return multiple values etc, but most of the time it will be a single value.
[2019-05-22 23:23:29.689] [I] Using PowerShell Aruments '-Servername xxxxxx -config_dir C:\TEMP -instance_no 02 -type TEST -admin_login xxxxxxxxxxxxxx -collation SQL_Latin1_General_CP1_CI_AS -sql_account "NT AUTHORITY\SYSTEM" -install_dir 😧 -sql_components "SQLENGINE,REPLICATION" -sql_dir H:\DATA01 -log_dir F:\LOG0 -backup_dir E:\BACKUPS -tempdb_dir X:\DIR'.
[2019-05-22 23:23:41.522] [I]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False CmdletBindingAttribute System.Management.Automation.CmdletCommonMetadataAttribute
LastWriteTime : 22/05/2019 23:23:39
Length : 1196
Name : ServerName_SQL_Configuration_File_2019_05_22_11_23.ini
PSComputerName : ServerName
C:\TEMP\ServerName_SQL_Configuration_File_2019_05_22_11_23.ini
The above is the output when I run the workflow.
The bit my Ps script returns is C:\TEMP\ServerName_SQL_Configuration_File_2019_05_22_11_23.ini and this is what I want to capture into a variable. and pass onto another workflow
am just looking at other options for running powershell code on a remote host. I bumped into the syntax below but was wondering what action I could use to implement it.
var psScript = ''
//#trap:
psScript +='\ntrap { \n';
psScript +=' foreach ($err in $error) {\n';
psScript +=' write-output $err\n';
psScript +=' }\n'
psScript +=' if ( $session -ne $null) {\n';
psScript +=' remove-pssession -session $session \n';
psScript +=' }\n';
psScript +=' exit 1\n';
psScript +='}\n';
psScript +='$ErrorActionPreference = \'Stop\'\n';
psScript +='import-module sqlps -WarningAction:SilentlyContinue -ErrorAction:Stop \n';
//#create session to targethost:
psScript +='$username = \''+buildaccount+'\'\n';
psScript +="$password = Get-Content '"+buildaccountpwdfile+"' | ConvertTo-SecureString\n";
psScript +='$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password\n';
psScript +='$session = new-pssession -computerName '+targethost+' -credential $credential -authentication credssp\n';
//obtain instance name
psScript +='$sqlserver_server_name = \'select servername from [dbo].[buildlog] where buildno=\'\''+buildno+'\'\'\'\n';
psScript +='$instancename = invoke-sqlcmd -QueryTimeout 30 -ConnectionTimeout 30 -ServerInstance '+buildinstance+' -Database inventory -Query $sqlserver_server_name\n';
psScript += 'get-pssession -ComputerName '+targethost+'| remove-pssession\n';
System.getModule("ModuleName").executePowershellDirectOnVCOServer(psScript, "Server Identification", null);
System.log(psScript);
OK, so this is the output value from your Powershell script that you need. As is stated in the documentation for PowerShellRemotePSObject#getRootObject() - "The result can be simple type, ArrayList, Properties or PowerShellPSObject". The output you provided shows that the result is an array with 3 elements, each of type PSObject. So if you need to, you can iterate over this array and process each element, using the attributes/methods available in PSObject scripting class.
I could not find any examples out there, the closest i saw was this, however not sure if i am on the right path.
var isList = mydata instanceof Array
if ( isList ){
for (idx in mydata){
var item = mydata[idx];
if ( item.instanceOf('System.IO.FileInfo') ){//check type of object
System.log( item.getProperty('FileName') );//extract value from result
}
}
} else {
System.log( mydata.getProperty('FileName') );//extract value from result
}