scriptblock

Powershell How to Modify Script to Hardcode “ | export-csv c:\temp\filename.csv -notypeinformation”

风流意气都作罢 提交于 2020-07-19 18:42:26
问题 I have this awesome script I use to generate a list of folders with their assigned security groups and each user in each group. When I run it, I type .\getfolderacls.ps1 -verbose | export-csv c:\temp\filename.csv -notypeinformation . That works perfectly, but I'd like to hardcode the | export-csv... part so that I can just run it without the arguments (or are they parameters?). I tried simply appending | export-csv c:\temp\test.csv -notypeinformation to the bottom of the script, but that

PowerShell Executing a function within a Script Block using Start-Process does weird things with double quotes

让人想犯罪 __ 提交于 2020-02-01 04:07:26
问题 I have a PowerShell script that edits the registry, so it needs to run as admin. To do this, I start up a new PowerShell process from my running PowerShell script, and pass in part of the registry key path using a Script Block with a function in it. When I use double quotes within that function, PowerShell tries to interpret them as commands, rather than a string. If I use single quotes though, then everything works fine. I've created a little stripped down sample powershell script that

How to call function outside a scriptblock

谁说我不能喝 提交于 2020-01-04 12:57:04
问题 I have a function that I should be able to call from any place in my powershell script. The problem is that it doesn't identify the function in a script block. In the following example I have the function getNumberFive which should return the number 5. I want to be able to use this function inside a scriptblock when I start a new job and also in the end of the script. Expected result: Write the number 15 to the file "C:\tmp\result.txt" Write to the console: "I like the number 5" In reality:

PowerShell ScriptBlock and multiple functions

非 Y 不嫁゛ 提交于 2020-01-02 12:45:55
问题 I have written the following code: cls function GetFoo() { function GetBar() { $bar = "bar" $bar } $foo = "foo" $bar = GetBar $foo $bar } $cred = Get-Credential "firmwide\srabhi_adm" $result = Invoke-Command -Credential $cred -ComputerName localhost -ScriptBlock ${function:GetFoo} Write-Host $result[0] Write-Host $result[1] It works but I don't want to define GetBar inside of GetFoo . Can I do something like this? cls function GetBar() { $bar = "bar" $bar } function GetFoo() { $foo = "foo"

Powershell Start-Process to start Powershell session and pass local variables

跟風遠走 提交于 2019-12-30 21:09:13
问题 Is there a way to use the Powershell Start-Process cmdlet to start a new Powershell session and pass a scriptblock with local variables (once of which will be an array)? Example: $Array = @(1,2,3,4) $String = "This is string number" $Scriptblock = {$Array | ForEach-Object {Write-Host $String $_}} Start-Process Powershell -ArgumentList "$Scriptblock" Thanks. 回答1: I'm pretty sure there's no direct way to pass variables from one PowerShell session to another. The best you can do is some

Powershell expand variable in scriptblock

寵の児 提交于 2019-12-29 07:59:15
问题 I am trying to follow this article to expand a variable in a scriptblock My code tries this: $exe = "setup.exe" invoke-command -ComputerName $j -Credential $credentials -ScriptBlock {cmd /c 'C:\share\[scriptblock]::Create($exe)'} How to fix the error: The filename, directory name, or volume label syntax is incorrect. + CategoryInfo : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError + PSComputerName : remote_computer 回答1:

Powershell call batch within scriptblock

十年热恋 提交于 2019-12-25 03:32:28
问题 I have the script below which is not working the way I want. Initially, I would like to pass install.cmd to function which will use the "Start-Job" in the background so that it doesn't freeze up the main Powershell window. But I can't get it to call the install.cmd. $Appname = @("Adobe_FlashPlayer", "Acrobat_Reader", "Microsoft_RDP") function BatchJob{ Param ( [ScriptBlock]$batchScript, $ArgumentList = $null) #Start the batch $batch = Start-Job -ScriptBlock $batchScript -ArgumentList

How to call outside defined function in runspace scriptblock

送分小仙女□ 提交于 2019-12-24 09:10:59
问题 I have a complex PowerShell function which I would like to run another threads. However, If I'm right, the function cannot be accessed in the scriptblock. I want to avoid to copy every related function next to it. Is there any way or method to call the function in the scriptblock? function Function1 { Param() Process { $param1 = "something" $pool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS + 1) $pool.ApartmentState = "MTA" $pool.Open() $runspaces = @()

Supporting lexical-scope ScriptBlock parameters (e.g. Where-Object)

风流意气都作罢 提交于 2019-12-23 09:22:03
问题 Consider the following arbitrary function and test cases: Function Foo-MyBar { Param( [Parameter(Mandatory=$false)] [ScriptBlock] $Filter ) if (!$Filter) { $Filter = { $true } } #$Filter = $Filter.GetNewClosure() Get-ChildItem "$env:SYSTEMROOT" | Where-Object $Filter } ################################## $private:pattern = 'T*' Get-Help Foo-MyBar -Detailed Write-Host "`n`nUnfiltered..." Foo-MyBar Write-Host "`n`nTest 1:. Piped through Where-Object..." Foo-MyBar | Where-Object { $_.Name -ilike

How to pass a named function as a parameter (scriptblock)

回眸只為那壹抹淺笑 提交于 2019-12-22 10:51:06
问题 Let's take the classic first-order functions example: function Get-MyName { "George" } function Say-Hi([scriptblock]$to) { Write-Host ("Hi "+(& $to)) } This works just fine: Say-Hi { "Fred Flintstone" } this does not: Say-Hi Get-MyName because Get-MyName is evaluated, not passed as a value itself. How do I pass Get-MyName as a value? 回答1: You have to pass Get-Myname as a scriptblock, because that's how you've defined the variable type. Say-Hi ${function:Get-MyName} 回答2: If you are ready to