小编alp*_*dev的帖子

PSCustomObject到Hashtable

将a转换PSCustomObject为a 的最简单方法是Hashtable什么?它显示就像一个带有splat运算符,花括号和看似是键值对的那个.当我尝试将它投射到[Hashtable]它不起作用.我也试过.toString(),分配的变量说它是一个字符串,但没有显示任何 - 任何想法?

powershell hashtable pscustomobject

64
推荐指数
4
解决办法
5万
查看次数

有没有办法从函数中检索PowerShell函数名?

例如:

function Foo { 
    [string]$functionName = commandRetrievesFoo
    Write-Host "This function is called $functionName"
}
Run Code Online (Sandbox Code Playgroud)

输出:

PS > Foo
This function is called foo
Run Code Online (Sandbox Code Playgroud)

reflection powershell function

19
推荐指数
3
解决办法
2万
查看次数

使用递归进行文件复制期间的powershell错误检查

我有一个程序可以递归地复制文件夹和文件。例子:

Copy-Item -path "$folderA" -destination "$folderB" -recurse 
Run Code Online (Sandbox Code Playgroud)

有时文件不会复制。有没有办法“步入递归”或更好的方法来做到这一点,这样我就可以在过程中而不是事后启用某种错误检查。甚至可能进行测试路径并提示重新复制?

error-handling powershell recursion copy-item

4
推荐指数
1
解决办法
2万
查看次数

powershell传递system.object []而不展开

关于我如何能够通过Get-PSCallStack进行展开的任何想法.它似乎是一个system.object [],但从我在网上阅读的内容来看,它们在传递和"展开"时并不完整.我尝试在前面放置一个逗号以防止它,但这不起作用.

function Pass-Callstack ([System.Object]$arg0) {
Write-Host 'Start Pass-Callstack'
$psCallStack = $arg0
$psCallStackType = $psCallStack.GetType()
$psCallStackLength = $psCallStack.Length
$psCallStackCommand0 = $psCallStack[0].command 
$psCallStackCommand1 = $psCallStack[1].command
Write-Host $psCallStackType
Write-Host $psCallStackLength
Write-Host $psCallStackCommand0
Write-Host $psCallStackCommand1
$arg0 | gm
}

function Describe-Callstack {
Write-Host 'Start Describe-Callstack'
$psCallStack = (Get-PSCallStack)
$psCallStackType = $psCallStack.GetType()
$psCallStackLength = $psCallStack.Length
$psCallStackCommand0 = $psCallStack[0].command 
$psCallStackCommand1 = $psCallStack[1].command
Write-Host $psCallStackType
Write-Host $psCallStackLength
Write-Host $psCallStackCommand0
Write-Host $psCallStackCommand1
$psCallStack | gm
}
Describe-Callstack
Pass-Callstack (,$psCallStack)
Run Code Online (Sandbox Code Playgroud)

powershell argument-passing

3
推荐指数
1
解决办法
1万
查看次数

Powershell错误返回哈希表

任何人都有任何想法为什么以下代码会产生错误,请参阅函数后的其他注释以获取更多详细信息

function callee    ([Hashtable]$arg0) {
    [Hashtable]$hashtable = @{}
    $hashtable = $arg0
    $hashtable.add('passed', $True)
    # $hashtable                            ######## toggle this line
    $type = $hashtable.GetType()
    Write-Host "$type"
    return $hashtable
}

function caller {
    [Hashtable]$hashtable = @{'00'='0'}
    $hashtable = callee $hashtable        ##### returns error here
    $hashtable.add('returned', $True)
    $hashtable
}
caller
Run Code Online (Sandbox Code Playgroud)

错误消息:无法将"System.Object []"类型的"System.Object []"值转换为"System.Collections.Hashtable"类型.

我收到了各种情况的错误,我试图将其缩小到一个易于重现的例子.看起来它正在将哈希表更改为对象数组,这就是为什么它不会返回它?它允许我修改哈希表并返回它但是当我尝试显示它时会改变它吗?这与我开始向被调用者函数添加代码时获得的效果相同?

powershell return hashtable

3
推荐指数
1
解决办法
6054
查看次数