none
Strange behavior on Power shell Scripting RRS feed

  • Question

  • Hello,

                 I tried couple of Lync Synthetic transaction test (PS commandlet) from remote machine thro PS Script

    Syntax: Test-CsPresence -TargetFqdn atl-cs-001.litwareinc.com -SubscriberSipAddress "sip:pilar@litwareinc.com" -SubscriberCredential $cred1 -PublisherSipAddress "sip:kenmyer@litwareinc.com" -PublisherCredential $cred2

    • The below Powershell commandlet is not working in our usual PS format when placing the entire syntax in -Scriptblock{Syntax}

                                    But when making something like below, its working and the installation getting started

                    “ try{

                    $cmd= Invoke – command -session $session –scriptblock {;}

                                    Test-CsPresence -TargetFqdn atl-cs-001.litwareinc.com -SubscriberSipAddress "sip:pilar@litwareinc.com" -SubscriberCredential $cred1 -PublisherSipAddress "sip:kenmyer@litwareinc.com" -PublisherCredential $cred2

                       }”

                    Wrtie-host $cmd

     

                    I donno why this strange behaviour. Kindly Someone help on this.

    Regards,

    Julie

    Tuesday, January 28, 2014 5:39 AM

Answers

  • Hi Julie,

    then what you want to output will likely be:

    Write-Host $cmd.Result

    or

    Write-Host ("" + $cmd.Target + ": " + $cmd.Result)

    Though if you want to run it unattended and have it dump the content to a file, you may want to look into the commands "Export-Csv" and "Export-Clixml", both of which are superior at outputting data over converting it to a single string and writing it line-by-line.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Juliesmiley Wednesday, January 29, 2014 10:36 AM
    Tuesday, January 28, 2014 2:58 PM

All replies

  • What strange behavior?  You also misspelled some things.

    What do you mean by "SYNTAX:"


    ¯\_(ツ)_/¯

    Tuesday, January 28, 2014 5:57 AM
  • Notworking:

    Try{
    $cmd=invoke-command -session $session -scriptblock{ Test-CsPresence -TargetFqdn atl-cs-001.litwareinc.com -SubscriberSipAddress "sip:pilar@litwareinc.com" -SubscriberCredential $cred1 -PublisherSipAddress "sip:kenmyer@litwareinc.com" -PublisherCredential $cred2 }
    Remove-PSSession -Session $Session;
    }
    Catch
    {
       $RetVal = "Error23: " + $error[0].Exception.toString()
    $cmd
    }

    Working:
    Try{
    $cmd=invoke-command -session $session -scriptblock{;}
    Test-CsPresence -TargetFqdn atl-cs-001.litwareinc.com -SubscriberSipAddress "sip:pilar@litwareinc.com" -SubscriberCredential $cred1 -PublisherSipAddress "sip:kenmyer@litwareinc.com" -PublisherCredential $cred2 
    Remove-PSSession -Session $Session;
    }
    Catch
    {
       $RetVal = "Error23: " + $error[0].Exception.toString()
    $cmd
    }

    Can you please clarify

    Tuesday, January 28, 2014 8:57 AM
  • Hi Julie,

    you can't use variables in an Invoked Scriptblock in another session that exist in the current session, unless you pass them as variables into it. Example:

    $script = {
        Param(
            $cred1,
            $cred2
        )
        Test-CsPresence -TargetFqdn atl-cs-001.litwareinc.com -SubscriberSipAddress "sip:pilar@litwareinc.com" -SubscriberCredential $cred1 -PublisherSipAddress "sip:kenmyer@litwareinc.com" -PublisherCredential $cred2
    }
     
    Try
    {
        $cmd = Invoke-Command -Session $session -ScriptBlock $script -ArgumentList $cred1,$cred2
        Remove-PSSession -Session $Session;
    }
    Catch
    {
        $RetVal = "Error23: " + [0].Exception.toString()
        $cmd
    }

    As shown in the example, you can make a scriptblock accept arguments, by setting that Param() block at the beginning of the script-block, and then set the ArgumentList parameter during the Invoke-Command cmdlet.

    Please note, that generally, posting code in codeblocks like this makes them easier to read and differentiate from regular text. Easier readable posts = Greater chance of useful responses to you :)

    The working example you posted worked, because Invoke-Command used an empty ( {;} does nothing) ScriptBlock, and you ran Test-CsPresence in the local session, not the $session you specified.

    Cheers,
    Fred


    There's no place like 127.0.0.1




    • Edited by FWN Tuesday, January 28, 2014 9:48 AM
    Tuesday, January 28, 2014 9:21 AM
  • Thanks,

    Here is my full Script and I totdally agree with the previous returned Output because i installed the Lync core files, so its locally executed,

    **************************************************************************

    $password=ConvertTo-SecureString -String "******" -AsPlainText -Force;

    $password1=ConvertTo-SecureString -String "*****" -AsPlainText -Force;

    $credential=New-Object System.Management.Automation.PSCredential("******", $password);

    $credential1=New-Object System.Management.Automation.PSCredential("******", $password1);

    $credential2=New-Object System.Management.Automation.PSCredential("********", $password1);

    $session=New-pssession -ComputerName "ABC.Julie.local" -Credential $credential -Port 5985 -Authentication Negotiate

    try{

    $cmd=invoke-command -session $session -scriptblock{Test-CsPresence –TargetFqdn XYZ.Julie.local -PublisherSipAddress "sip:******" -PublisherCredential $credential1 -SubscriberSipAddress "sip:****" -SubscriberCredential $credential2 -RegistrarPort 5061}



    Write-Host $cmd

    Remove-PSSession -Session $Session;

    }

    Catch

    {

      $RetVal="Error23: " + $error[0].Exception.toString()

    $cmd

    }

    ********************************************************************************

    Question: Its not returning the exact Output instead the below line "Microsoft.Rtc.SyntheticTransactions.TaskOutput object"

    Someone please help me to understand the same.

       
    Tuesday, January 28, 2014 2:14 PM
  • Hi Julie,

    Test-CsPresence returns an object of the type "Microsoft.Rtc.SyntheticTransactions.TaskOutput". Writing an object with Write-Host will call its ToString() method, that all objects inherit from the base object class. Unless the developer overrides the standard ToString function, it will return the object type, not any content. If you really need to write it to the host, try something like this:

    $cmd | FL * | Write-Host

    To see what properties and functions it actually has, try this:

    $cmd | Get-Member

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, January 28, 2014 2:33 PM
  • Thanks Fred :)

     Actually I have to get the Output from the above script and have to check whether it contains success or failure to move next activity, I will try your suggestion tomorrow and revert back.

    When i execute locally on Lync front end machine the script will return something like the below

    "Target : FQDN of Lync front machine

      Result : Success or failure"

    Regards,
    Julie

    Tuesday, January 28, 2014 2:53 PM
  • Hi Julie,

    then what you want to output will likely be:

    Write-Host $cmd.Result

    or

    Write-Host ("" + $cmd.Target + ": " + $cmd.Result)

    Though if you want to run it unattended and have it dump the content to a file, you may want to look into the commands "Export-Csv" and "Export-Clixml", both of which are superior at outputting data over converting it to a single string and writing it line-by-line.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Juliesmiley Wednesday, January 29, 2014 10:36 AM
    Tuesday, January 28, 2014 2:58 PM
  • Thanks for your Great Help. I would like to Powershell from the Scratch..can you help me with any link or source

    Wednesday, January 29, 2014 10:39 AM
  • Hi Julie,

    glad to have been of assistance :)

    There's some basic information and tutorial source here in the scripting center.
    In the Script Repository, you can find many scripts. Beware however, for this is treacherous. You can look up some things, when you want to know how to do some specific task. However:
    - Those scripts not only make functionality immortal - they also make errors or bad habits immortal.
    - Relying on the actual script can actually inhibit learning, so only use them for reference when searching for a specific technical action.

    Learning PowerShell 3 in a Month of lunches is a good book to get started too, in my opinion.

    Except for those sources however, I can give some advice on getting started myself:

    1. PowerShell is both Scripting language and commandline interface. Do not use it exclusively as one of those, but rather in both roles. You can load scripts into a running PowerShell session (Import-Module C:\ExampleFolder\ExampleScript.ps1).
    2. There are three commands you really need to know, if you want to learn to do it yourself: Get-Help, Get-Member and Get-Command. Try these commands in the PS CommandLine:
      Get-Help Get-Help
      Get-Help Get-Member
      Get-Help Get-Command
      Even if it may seem a bit dry, read them fully. These are the most important tools in all of PowerShell when it comes to learning it on your own.
    3. Try out the following command:
      Get-Command "*-Object"
      This will give you a list of commands, that are very useful in handling the output of other commands. From sorting content over filtering out some or finding elements by specific criteria to Selecting only specific aspects of individual return objects.

    There's a lot more than just this - I could literally go on for days - but overloading you with info right away isn't going to be doing you any favors. Good luck on your way to learning the PowerShell, when you have another specific question - not necessarily restricted to on a script, but about an aspect of PowerShell itself - we'll be here.

    Cheers,
    Fred

    Ps.: We all still keep learning new stuff - there's rarely a day I don't learn something new about the PowerShell myself :)


    There's no place like 127.0.0.1


    • Edited by FWN Wednesday, January 29, 2014 12:41 PM
    Wednesday, January 29, 2014 12:31 PM
  • Thanks a lot for your Great help Fred, will start right away with the basics:)


    Wednesday, January 29, 2014 2:25 PM