Need help

This topic contains 6 replies, has 5 voices, and was last updated by  Rob Simmers 2 years, 5 months ago.

  • Author
    Posts
  • #27479

    Harshall mankar
    Participant

    I want to write swapping script which can swap 2 parameters
    which cmdlets do I need do hat

  • #27488

    kvprasoon
    Participant

    Hi Harshall,
    Can you Provide Little More details.
    Atleast a sample script.

    Regards,
    kvprasoon

  • #27494

    Garth Maynard
    Participant

    Depending on what you are trying to swap, there are a couple of ways to go about it.

    (Get-Content $file) | Foreach-Object { $_ -replace $SearchArray[$i], $ReplaceArray[$i] } | Set-Content $file

    is one way. If you simple want to swap a word in a string you can use:

    $a = $a -replace "CurrentWord", "NewWord"

  • #27516

    Harshall mankar
    Participant

    function get-swap{
    [CmdletBinding()]
    param(
    [parameter(mandatory=$true)]$param1,
    [parameter(mandatory=$true)]$param2
    )

    ""
    "Original Vaues are "+$param1+" & "+$param2
    $temp1=$param1
    $param1=$param2
    $param2=$temp1
    ""
    "swapped Vaues are $param1 & $param2"

    }

    this I tried and worked
    as I am new to this but
    my question is that is so simple and this can be done in any programming language with same syntax so how can I reduce the code in power shell Or in other ways how can it get easy than this

  • #27519

    Simon Wåhlin
    Participant

    In PowerShell you can assign a list of values to multiple variables, for example:

    $a,$b = 'first','second'

    $a will be assigned the first value and $b the second value.
    This can be used to swap values on to variables.

    Fo your example, try this:

    function Get-Swap{
        [CmdletBinding()]
        param(
            [parameter(mandatory=$true)]$param1,
            [parameter(mandatory=$true)]$param2
        )
        "Original Vaues are $param1 & $param2"
        $param1,$param2=$param2,$param1
        "Swapped Vaues are $param1 & $param2" 
    }
    
  • #27521

    Harshall mankar
    Participant

    @ Simon Wåhlin Thanks Very much that was knowledgeable and exciting 2 🙂

  • #27525

    Rob Simmers
    Participant

    @simon. That's a new on me as well, thanks for sharing I can check off the "Learned something new" checkbox today. 🙂

You must be logged in to reply to this topic.