简体   繁体   English

ISE 中的 Powershell Get-Childitem

[英]Powershell Get-Childitem in ISE

please can you help me.请你能帮帮我吗? I'm missing some information.我缺少一些信息。

Why when I execute the code:为什么当我执行代码时:

Invoke-Command -ComputerName domainpc -ScriptBlock {Get-ChildItem -path C:\Users -Filter "username1"} 

the result is: username1结果是: username1

And when I execute this script:当我执行这个脚本时:

    $user = Read-Host "Please enter username"
Invoke-Command -ComputerName domainpc -ScriptBlock {Get-ChildItem -path C:\Users -Filter "$user"} 

the result is a list of folder contained in C:\\Users结果是包含在 C:\\Users 中的文件夹列表

I don't understand why executing script get different results than execute code without variable.我不明白为什么执行脚本得到的结果与执行没有变量的代码不同。 Seems that the problem is the variable.似乎问题出在变量上。 Please can you explain this?请你能解释一下吗?

Thanks.谢谢。

You are hitting scope "problems".您遇到了范围“问题”。
The variable $user inside your script block -ScriptBlock is not known.脚本块-ScriptBlock的变量$user未知。
Take a look here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.1#using-local-variables看看这里: https : //docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.1#using-local-variables

Use $Using:user to get around.使用$Using:user绕过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM