logo
down
shadow

Encapsulate strings in an array in PowerShell


Encapsulate strings in an array in PowerShell

By : user2953986
Date : November 21 2020, 07:31 AM
will be helpful for those in need Unfortunately just setting the separator will only set the quotes between elements. It's going to miss the first and last quote.
code :
$target = 'Bob','Ben'

$target -replace '^|$','"' -join ','

"Bob","Ben"


Share : facebook icon twitter icon
PowerShell array to store strings

PowerShell array to store strings


By : J.zoo
Date : March 29 2020, 07:55 AM
I hope this helps . I'd suggest working with objects instead of a lot of string arrays. I have an example below in which I have replaced the file listing, since I don't have the file structure in place, with an ordinary array. Just remove that array declaration and put in your Get-ChildItem call and it should work just fine.
code :
function ConvertTo-MyTypeOfItem
{
    PARAM (
        [ValidatePattern("([^_]+_){3}[^_]+")]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$StringToParse
    )

    PROCESS {
        $custId, $invId, $prodId, [int]$value = $StringToParse -split "_"
        $myObject = New-Object PSObject -Property @{
            CustomerID = $custId;
            InvoiceID = $invId;
            ProductID = $prodId;
            Value = $value
        }
        Write-Output $myObject
    }
}

# In the test scenario I have replaced getting the list of files
# with an array of names. Just uncomment the first and second lines 
# following this comment and remove the other $baseNames setter, to
# get the $baseNames from the file listing

#$files = Get-ChildItem test *.txt
#$baseNames = $files.BaseName
$baseNames = @(
    "cust1_inv1_prod1_1";
    "cust2_inv2_prod2_2";
    "cust3_inv3_prod3_3";
    "cust4_inv4_prod4_4";
)

$myObjectArray = $baseNames | ConvertTo-MyTypeOfItem 

$myObjectArray
InvoiceID                CustomerID               ProductID                                  Value
---------                ----------               ---------                                  -----
inv1                     cust1                    prod1                                          1
inv2                     cust2                    prod2                                          2
inv3                     cust3                    prod3                                          3
inv4                     cust4                    prod4                                          4
How do I get an array of strings from a powershell pipeline?

How do I get an array of strings from a powershell pipeline?


By : anthony zinnel
Date : March 29 2020, 07:55 AM
I hope this helps you . object[] is simply the default array in PowerShell, and it doesn't matter in this (and most) situations. You problem is that you're trying to join to arrays using (arr1, arr2). What this acutally does is create an array with two array objects, because , is an array construction operator. Try to join the arrays using +, like this:
code :
$dirs = 'dir1', 'dir2', 'dir3'
$paths = $dirs | % { Resolve-Path -ea 0 $_ } | Select -ExpandProperty Path    

$newpath = (($env:PATH -split ';') + $paths) -join ';'
$dirs = 'dir1', 'dir2', 'dir3'
$paths = $dirs | % { Resolve-Path -ea 0 $_ } | Select -ExpandProperty Path
$newpath = "$env:PATH;$($paths -join ';')"
Encapsulate the output of invoke command in a variable - PowerShell

Encapsulate the output of invoke command in a variable - PowerShell


By : Vio B
Date : March 29 2020, 07:55 AM
hope this fix your issue Generally for powershell to return something from a command\function, you want to produce any kind of output. So just "bla-bla" inside your code will return "bla-bla" to the caller. With that your case simplified:
code :
$res = Invoke-Command -ComputerName "testpc.eil.local" -ScriptBlock {
    (Get-WindowsFeature -Name "Remote-Desktop-Services").Installed
}

do something with $res here
What are the advantages of using callbacks over strings to encapsulate code in javascript?

What are the advantages of using callbacks over strings to encapsulate code in javascript?


By : SarahM
Date : March 29 2020, 07:55 AM
Hope this helps eval itself does not create a closure and does not provide encapsulation. It is merely a means to evaluate JavaScript code represented as a string.
If you use eval to execute a function (like your example does), it will not modify the scope of that function nor the closure created by that function. Regardless of eval, functions always execute in the scope in which they were originally declared and they always create a closure (which encapsulates any declarations made within that function).
code :
function test() {
  var x = 2, y = 4;
  console.log(eval('x + y'));  // Direct call, uses local scope, result is 6
  var geval = eval; // equivalent to calling eval in the global scope
  console.log(geval('x + y')); // Indirect call, uses global scope, throws ReferenceError because `x` is undefined
}
Identify if any string in one array exists in second array of strings with PowerShell

Identify if any string in one array exists in second array of strings with PowerShell


By : Haze
Date : March 29 2020, 07:55 AM
this one helps. Nathan Rice has a simpler answer but since you mention -contains and -match I thought I could show you a couple of ways to make that work. Given your sample strings
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org