Powershell 测试路径的问题
我试图远程枚举Windows(管理)共享,然后对每个共享进行Powershell 测试路径的问题,powershell,Powershell,我试图远程枚举Windows(管理)共享,然后对每个共享进行测试路径,以验证是否存在文件夹。 问题是测试路径仅当运行powershell.exe的帐户具有查看相关文件夹的权限时,才会返回“True”。 因此,我试图打开一个新的powershell.exe,并在具有权限的用户的上下文中运行脚本 #share enumeration and Test-Path $scriptBlock = { Param ([System.Management.Automation.PSCredential
测试路径
,以验证是否存在文件夹。
问题是测试路径
仅当运行powershell.exe
的帐户具有查看相关文件夹的权限时,才会返回“True”。
因此,我试图打开一个新的powershell.exe
,并在具有权限的用户的上下文中运行脚本
#share enumeration and Test-Path
$scriptBlock = {
Param ([System.Management.Automation.PSCredential]$cred)
$shares = Get-WmiObject -Class Win32_Share -ComputerName COMPUTER -Credential $cred
$sharename = $shares.Name
$sharename #shares are correctly enumerated
foreach ($name in $sharename) {
$name1 = '\\COMPUTER' + $name + '\FOLDER'
$name1 #UNC location is correctly set e.g. \\COMPUTER\d$\FOLDER
$path = Test-Path -Path $name1
Write-Host $path #always returns FALSE
}
}
$username = "user"
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock} $cred"
Start Process
命令未正确接收存储在$cred
中的凭据,而是请求凭据。即使提供了正确的凭据,测试路径
的输出对于所有共享返回FALSE。
powershell.exe
在当前用户的上下文中运行。
如果我将当前用户添加为远程计算机的管理员,testpath
将为包含“\FOLDER”的位置返回TRUE。
这个场景不是我想要实现的,因为我们将通过一个框架远程运行这个脚本。不要在脚本中使用纯文本密码,尤其是在使用管理凭据时。您将自己和组织暴露在不必要的风险中 如果您不想在脚本运行期间收到提示输入cred,那么您需要提前以安全的方式存储它们,并从那里调用它们。web上有很多关于在PowerShell脚本中使用、Windows凭据管理器、安全XML文件甚至注册表保护凭据的文章 这个
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock} $cred"
。。。语法无效。应该是这个
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock}" -Credential $Cred
# share enumeration and Test-Path
# Using variable squeezing to assign and output variable, debug validation effort
# $scriptBlock = {
# Pick a random AD computer and show all shares
"`n***"
"*** List all share data ***"
"***`n"
($shares = Get-WmiObject -Class Win32_Share -ComputerName (Get-ADComputer -Filter '*').Name[7])
# Process test validation
"`n***"
"*** Testing share path ***"
"***`n"
foreach ($share in $shares)
{ ($sharename = "\\$($share.PSComputerName)\$($share.Name)") + ' : ' + ($path = Test-Path -Path $sharename)}
# }
-它确实具有凭据属性
最后,如果你是在你的工作站上运行这个,而你有烫发来做这件事,那么你为什么要通过信用卡呢?
您的交互式登录,如果您使用的帐户在目标上具有perms,则它将正常工作,您根本不需要启动过程
例如,只是这样做
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock}" -Credential $Cred
# share enumeration and Test-Path
# Using variable squeezing to assign and output variable, debug validation effort
# $scriptBlock = {
# Pick a random AD computer and show all shares
"`n***"
"*** List all share data ***"
"***`n"
($shares = Get-WmiObject -Class Win32_Share -ComputerName (Get-ADComputer -Filter '*').Name[7])
# Process test validation
"`n***"
"*** Testing share path ***"
"***`n"
foreach ($share in $shares)
{ ($sharename = "\\$($share.PSComputerName)\$($share.Name)") + ' : ' + ($path = Test-Path -Path $sharename)}
# }
你会得到这个
# Results
***
*** List all share data ***
***
Name Path Description
---- ---- -----------
ADMIN$ C:\Windows Remote Admin
C$ C:\ Default share
install C:\install
IPC$ Remote IPC
print$ C:\Windows\system32\spool\drivers Printer Drivers
***
*** Testing share path ***
***
\\LabServer01\ADMIN$ : True
\\LabServer01\C$ : True
\\LabServer01\install : True
\\LabServer01\IPC$ : False
\\LabServer01\print$ : True
现在,如果你登录的信用卡没有烫发,那么,是的,通过他们
($cred = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME")
。。。在启动过程中,如果启用了PSRemoting,甚至更好
($cred = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME")
Invoke-Command -ComputerName $((Get-ADComputer -Filter '*').Name[7]) -ScriptBlock {
# Pick a random AD computer and show all shares
"`n***"
"*** List all share data on $env:COMPUTERNAME ***"
"***`n"
($shares = Get-WmiObject -Class Win32_Share -ComputerName $env:COMPUTERNAME)
# Process test validation
"`n***"
"*** Testing share path ***"
"***`n"
$shares | ForEach-Object {
Write-Host "Testing $($PSItem.Name) : " -NoNewline
Try {Test-Path -Path $PSItem.Path}
Catch{"Path for $($PSItem) is empty"}
}
} -Credential $cred
# Results
***
*** List all share data on LabServer01 ***
***
Name Path Description PSComputerName
---- ---- ----------- --------------
ADMIN$ C:\Windows Remote Admin LabServer01
C$ C:\ Default share LabServer01
install C:\install LabServer01
IPC$ Remote IPC LabServer01
print$ C:\Windows\system32\spool\drivers Printer Drivers LabServer01
***
*** Testing share path ***
***
Testing ADMIN$ : True
Testing C$ : True
Testing install : True
Testing IPC$ : Path for Cannot bind argument to parameter 'Path' because it is an empty string. is empty
Testing print$ : True
不要在脚本中使用纯文本密码,尤其是在使用管理凭据时。您将自己和组织暴露在不必要的风险中 如果您不想在脚本运行期间收到提示输入cred,那么您需要提前以安全的方式存储它们,并从那里调用它们。web上有很多关于在PowerShell脚本中使用、Windows凭据管理器、安全XML文件甚至注册表保护凭据的文章 这个
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock} $cred"
。。。语法无效。应该是这个
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock}" -Credential $Cred
# share enumeration and Test-Path
# Using variable squeezing to assign and output variable, debug validation effort
# $scriptBlock = {
# Pick a random AD computer and show all shares
"`n***"
"*** List all share data ***"
"***`n"
($shares = Get-WmiObject -Class Win32_Share -ComputerName (Get-ADComputer -Filter '*').Name[7])
# Process test validation
"`n***"
"*** Testing share path ***"
"***`n"
foreach ($share in $shares)
{ ($sharename = "\\$($share.PSComputerName)\$($share.Name)") + ' : ' + ($path = Test-Path -Path $sharename)}
# }
-它确实具有凭据属性
最后,如果你是在你的工作站上运行这个,而你有烫发来做这件事,那么你为什么要通过信用卡呢?
您的交互式登录,如果您使用的帐户在目标上具有perms,则它将正常工作,您根本不需要启动过程
例如,只是这样做
Start-Process powershell -ArgumentList "-noexit -command & {$scriptBlock}" -Credential $Cred
# share enumeration and Test-Path
# Using variable squeezing to assign and output variable, debug validation effort
# $scriptBlock = {
# Pick a random AD computer and show all shares
"`n***"
"*** List all share data ***"
"***`n"
($shares = Get-WmiObject -Class Win32_Share -ComputerName (Get-ADComputer -Filter '*').Name[7])
# Process test validation
"`n***"
"*** Testing share path ***"
"***`n"
foreach ($share in $shares)
{ ($sharename = "\\$($share.PSComputerName)\$($share.Name)") + ' : ' + ($path = Test-Path -Path $sharename)}
# }
你会得到这个
# Results
***
*** List all share data ***
***
Name Path Description
---- ---- -----------
ADMIN$ C:\Windows Remote Admin
C$ C:\ Default share
install C:\install
IPC$ Remote IPC
print$ C:\Windows\system32\spool\drivers Printer Drivers
***
*** Testing share path ***
***
\\LabServer01\ADMIN$ : True
\\LabServer01\C$ : True
\\LabServer01\install : True
\\LabServer01\IPC$ : False
\\LabServer01\print$ : True
现在,如果你登录的信用卡没有烫发,那么,是的,通过他们
($cred = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME")
。。。在启动过程中,如果启用了PSRemoting,甚至更好
($cred = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME")
Invoke-Command -ComputerName $((Get-ADComputer -Filter '*').Name[7]) -ScriptBlock {
# Pick a random AD computer and show all shares
"`n***"
"*** List all share data on $env:COMPUTERNAME ***"
"***`n"
($shares = Get-WmiObject -Class Win32_Share -ComputerName $env:COMPUTERNAME)
# Process test validation
"`n***"
"*** Testing share path ***"
"***`n"
$shares | ForEach-Object {
Write-Host "Testing $($PSItem.Name) : " -NoNewline
Try {Test-Path -Path $PSItem.Path}
Catch{"Path for $($PSItem) is empty"}
}
} -Credential $cred
# Results
***
*** List all share data on LabServer01 ***
***
Name Path Description PSComputerName
---- ---- ----------- --------------
ADMIN$ C:\Windows Remote Admin LabServer01
C$ C:\ Default share LabServer01
install C:\install LabServer01
IPC$ Remote IPC LabServer01
print$ C:\Windows\system32\spool\drivers Printer Drivers LabServer01
***
*** Testing share path ***
***
Testing ADMIN$ : True
Testing C$ : True
Testing install : True
Testing IPC$ : Path for Cannot bind argument to parameter 'Path' because it is an empty string. is empty
Testing print$ : True
您是否已尝试将
测试路径
?“-noexit-command&{$scriptBlock}$cred”
的-Credential
参数转换为字符串。这意味着您正在有效地将“System.Management.Automation.PSCredential”传递到scriptblock中。您不需要运行第二个PowerShell进程。Invoke-Command可能是一个更好的选项,因为它有一个credentials参数$Creds
变量需要有一个要绑定的参数。该参数是-Credential
。[grin]您使用的位置使cmdlet尝试将$Creds
分配给参数列表。。。因此,一如既往,强烈建议您使用完整的参数名。另外,在这种情况下,您可能会将参数列表的内容放在最后,以减少混淆的可能性。您是否尝试过将测试路径的-Credential
参数?“-noexit-command&{$scriptBlock}$cred”
转换为字符串。这意味着您正在有效地将“System.Management.Automation.PSCredential”传递到scriptblock中。您不需要运行第二个PowerShell进程。Invoke-Command可能是一个更好的选项,因为它有一个credentials参数$Creds
变量需要有一个要绑定的参数。该参数是-Credential
。[grin]您使用的位置使cmdlet尝试将$Creds
分配给参数列表。。。因此,一如既往,强烈建议您使用完整的参数名。另外,在这种情况下,您可能会将参数列表的内容放在最后,这样就不会有太多的混淆。ello,谢谢大家的宝贵意见。我们不会将凭据存储在脚本正文中。我们将使用一个框架来加密凭据并将其安全地传递给脚本;)-将测试路径与-Credentials参数一起使用会产生错误“测试路径:无法检索cmdlet的动态参数。文件系统提供程序仅支持新的PSDrive cmdlet上的凭据。请在不指定凭据的情况下再次执行该操作。”关于启动进程功能,如果您记住,如果一个用户要在另一个用户的上下文中运行程序,那么该用户也应该出现在本地系统上,那么它就起作用了。因此,添加一个与特权远程用户具有相同用户名/密码的本地用户就可以做到这一点。这本来应该是一项非常简单的任务,后来变成了骇客们讨厌的丑陋东西——我现在正在重新思考整个设计。再次感谢你们,祝你们度过愉快的一周!谢谢大家的宝贵意见。我们不会将凭据存储在脚本正文中。我们将使用一个框架来加密凭证并传递t