Powershell's Schziophrenic Behaviors

apr911apr911 Member Posts: 380 ■■■■□□□□□□
Having moved back in to a predominately Windows-centric role after years in a Network-centric role, Im trying hard to bring my powershell skills up to speed but Im finding it incredibly hard to do.

Not because Powershell is intrinsically difficult to learn but, in true Microsoft fashion, Powershell cant seem to decide if it wants to be a programming language, a scripting language or its own thing. As a result, some things dont operate the way one might expect...

Here are 3 examples:
$a = "MyString"
$b = $a
$a = "MyChangedString"
Write-Host $b
MyString

All good there, pretty standard for a scripting language but...
$a = @( "MyString1", "MyString2", "MyString3" )
$b = $a
$a.remove(1)
Write-Host $b
MyString1 MyString3

With complex variables powershell follows the more higher order programming languages and instead of copying values, just creates a memory pointer. To copy the variable you have to use $b=$a.clone() and if that weren't enough there's this:

[array]$a= @("MyString1", "MyString2", "MyString3" )
$a.remove(0)
$a.remove(1)
$a.GetType()

Type
String

I've programmed in a fair number of languages and to my recollection, I have never had a strong typecasted variable convert to something else.



I have a last example and one which I could use the communities help with trying to figure out. Im trying to write a script to automate MSSQL Install/Upgrades on Clusters. When I run this on a local host:

$instance=MyInstance
.\setup.exe /Action=Upgrade /Instance=$instance /MoreSwitches /IndicateProgress=1 | tee Log.log

I see a log file get written and a whole bunch of screen output as SQL installs.

When I put it into a function locally sourced function and then use invoke-command from a remote host:

LocalHost:
function Run-Install () {
param (
$instance
)
.\setup.exe /Action=Upgrade /Instance=$instance /MoreSwitches /IndicateProgress=1 | tee Log.log

}


RemoteHost:
icm -Session $ClusterNode -ScriptBlock { Run-install -instance MyInstance }

The setup program appears to startup (I have the setup program log in C:\Program Files\MSSQL\ ) but the prompt returns before the setup program finishes and continues to the next part of the script. The setup executable also dies never completing.


Thoughts?

Im going to try playing with jobs today to see if executing it as a job changes things but the fact that the output in a powershell window doesnt match the output in a powershell script is driving me insane (Hashtables are particularly difficult with this) and I miss python.
Currently Working On: Openstack
2020 Goals: AWS/Azure/GCP Certifications, F5 CSE Cloud, SCRUM, CISSP-ISSMP

Comments

  • apr911apr911 Member Posts: 380 ■■■■□□□□□□
    Answered my own question...

    It seems in a remote session, powershell will not write to the screen the same way it does in a local session but taking the output and continuing to do something with it works just fine.

    This is the command I created that functions:

    .\setup.exe /Action=Upgrade /Instance=$instance /MoreSwitches /IndicateProgress=1 | tee Log.log | foreach-object { Write-Host $_ > $null }

    The foreach is what keeps the command open. I suppose I could still execute the command as a job and monitor the job status though Im not sure if the job would have similar issues.
    Currently Working On: Openstack
    2020 Goals: AWS/Azure/GCP Certifications, F5 CSE Cloud, SCRUM, CISSP-ISSMP
Sign In or Register to comment.