Author Posts

August 17, 2018 at 12:14 am

 

Hello, I have been running the same code previously and I never saw the 3 properties PSComputerName, RunspaceId and PSShowComputerNamereturn return previously but now all of a sudden, I see them. I am stumped as to why?? The only difference was that I was running PS ver 5.0 and now its 5.1.

Please help me understand and eliminate these 3 properties from returning. This has caused my execution time to jump from 3 secs to 45secs approx.

Code and output below:

Invoke-Command -ComputerName SERVER -ScriptBlock {[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
>> $server = New-Object Microsoft.SqlServer.Management.Smo.Server "SERVER"
>> $server.Databases |
>> Select @{label="Server";Expression={$_.Parent.name}},
>> @{Label="DB_ID";Expression={$_.ID}},
>> @{label="Database Name";Expression={$_.Name}},
>> LastLogBackupDate,
>> LastDifferentialBackupDate,
>> LastBackupDate} | Sort-Object DB_ID | Format-Table * -AutoSize

Server      DB_ID Database Name       LastLogBackupDate    LastDifferentialBackupDate LastBackupDate       PSComputerName RunspaceId                           PSShowComputerName
------      ----- -------------       -----------------    -------------------------- --------------       -------------- ----------                           ------------------
SERVER     1 master              1/1/0001 12:00:00 AM 1/1/0001 12:00:00 AM       8/15/2018 7:04:00 PM SERVER            f79a0d6b-a96d-4aa5-89b2-1bcfed3ad569               True
SERVER     2 tempdb              1/1/0001 12:00:00 AM 1/1/0001 12:00:00 AM       1/1/0001 12:00:00 AM SERVER            f79a0d6b-a96d-4aa5-89b2-1bcfed3ad569               True
SERVER     3 model               1/1/0001 12:00:00 AM 8/15/2018 7:01:28 PM       8/10/2018 7:01:19 PM SERVER            f79a0d6b-a96d-4aa5-89b2-1bcfed3ad569               True
SERVER     4 msdb                1/1/0001 12:00:00 AM 8/15/2018 7:01:53 PM       8/10/2018 7:01:45 PM SERVER            f79a0d6b-a96d-4aa5-89b2-1bcfed3ad569               True

 

I also did a Get-Member on it :

Invoke-Command -ComputerName SERVER -ScriptBlock {[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
$server = New-Object Microsoft.SqlServer.Management.Smo.Server "SERVER"
$server.Databases |
Select @{label="Server";Expression={$_.Parent.name}},
@{Label="DB_ID";Expression={$_.ID}},
@{label="Database Name";Expression={$_.Name}},
LastLogBackupDate,
LastDifferentialBackupDate,
LastBackupDate} | GM


TypeName: Deserialized.Selected.Microsoft.SqlServer.Management.Smo.Database

Name                       MemberType   Definition
----                       ----------   ----------
Equals                     Method       bool Equals(System.Object obj)
GetHashCode                Method       int GetHashCode()
GetType                    Method       type GetType()
ToString                   Method       string ToString()
Database Name              NoteProperty string Database Name=ApexSQLCrd
DB_ID                      NoteProperty int DB_ID=5
LastBackupDate             NoteProperty datetime LastBackupDate=8/10/2018 7:03:22 PM
LastDifferentialBackupDate NoteProperty datetime LastDifferentialBackupDate=8/15/2018 7:04:04 PM
LastLogBackupDate          NoteProperty datetime LastLogBackupDate=8/16/2018 6:46:46 PM
PSComputerName             NoteProperty string PSComputerName=SERVER
PSShowComputerName         NoteProperty bool PSShowComputerName=True
RunspaceId                 NoteProperty guid RunspaceId=cb9605b8-7bda-44b5-bdc6-2af23f9a8efc
Server                     NoteProperty string Server=SERVER

August 17, 2018 at 12:35 am

This question has been answered already. You could have found it by yourself! 😉

https://powershell.org/forums/topic/invoke-command-remove-pscomputername/

August 17, 2018 at 2:31 am

Hello Olaf, thank you for the reply. This makes sense but what's really bothering me is that the fact that I didn't have these properties coming back previously, I mean like literally this started happening after I upgraded my system to the windows upgrade 1803 this morning. Could this be a reason?? I really want to duplicate the issue but the life of me I am not able to.

So that is what really bothered me.

Also, when I use Invoke-Command -FilePath those properties are not returned. How come? The file does not contain -ExcludeProperty.

 Invoke-Command -FilePath C:\Users\MSA050415\Documents\WindowsPowerShell\Scripts\Invoke_cmd_bkup.ps1 -ComputerName SERVER 

Server      DB_ID Database Name                       LastLogBackupDate     LastDifferentialBackupDate LastBackupDate
------      ----- -------------                       -----------------     -------------------------- --------------
SERVER master                              1/1/0001 12:00:00 AM  1/1/0001 12:00:00 AM       8/16/2018 8:06:24 PM
SERVER 2 tempdb                              1/1/0001 12:00:00 AM  1/1/0001 12:00:00 AM       1/1/0001 12:00:00 AM
SERVER 3 model                               1/1/0001 12:00:00 AM  8/16/2018 8:05:23 PM       8/10/2018 8:05:26 PM
SERVER 4 msdb                                1/1/0001 12:00:00 AM  8/16/2018 8:05:49 PM       8/10/2018 8:06:01 PM


August 17, 2018 at 5:05 am

Yes, Windows updates(Win 10/2016) will have changes(fixes and improvements) for PowerShell engine.

August 18, 2018 at 12:21 am

I figured out what went wrong, instead of filtering everything inside the ScriptBlock, I was filtering outside the ScriptBlock so that's why the 3 properties were showing up. All I had to do was to put the } at the end of the line. see code examples below.

Code that returned the 3 properties.

Invoke-Command.... -ScriptBlock {...
LastBackupDate } | Sort-Object DB_ID | Format-Table * -AutoSize

Code that did not returned the 3 properties.

 Invoke-Command.... -ScriptBlock {...
LastBackupDate | Sort-Object DB_ID | Format-Table * -AutoSize }