Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从远程计算机获取特殊文件夹_Powershell_Get Wmiobject - Fatal编程技术网

Powershell 从远程计算机获取特殊文件夹

Powershell 从远程计算机获取特殊文件夹,powershell,get-wmiobject,Powershell,Get Wmiobject,有没有办法从远程机器获取特殊文件夹 我正在使用以下代码获取本地文件夹: $path = [environment]::getfolderpath("CommonApplicationData") 但我想用这样的函数从其他机器上获取它: $specialFolder = Get-RemoteSpecialFolder "MyRemoteMachine" "CommonApplicationData" function Get-RemoteSpecialFolder { param(

有没有办法从远程机器获取特殊文件夹

我正在使用以下代码获取本地文件夹:

$path = [environment]::getfolderpath("CommonApplicationData")
但我想用这样的函数从其他机器上获取它:

$specialFolder = Get-RemoteSpecialFolder "MyRemoteMachine" "CommonApplicationData"

function Get-RemoteSpecialFolder
{
    param( 
        [string]$ComputerName,
        [string]$SpecialFolderName
    )
    Get-WMIObject ...
    ...
}

您可以通过读取远程计算机注册表(当然您需要权限)获得此信息,如使用以下功能:

function Get-RemoteSpecialFolder {
    [CmdletBinding()]
    param(
        [string]$ComputerName = $env:COMPUTERNAME,

        [string]$SpecialFolderName = 'Common AppData'
    )
    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) {
        $regPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
        try {
            $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
            $regKey  = $baseKey.OpenSubKey($regPath)
            return $regkey.GetValue($SpecialFolderName)
        }
        catch {
            throw
        }
        finally {
            if ($regKey)  { $regKey.Close() }
            if ($baseKey) { $baseKey.Close() }
        }
    }
    else {
        Write-Warning "Computer '$ComputerName' is off-line or does not exist."
    }
}
您应该能够通过以下方法找到这些常见的环境变量:

另外,先把函数放进去,然后调用它

$specialFolder = Get-RemoteSpecialFolder "MyRemoteMachine" "Common AppData"

谷歌搜索的第一次成功给了我这个脚本。。您可以使用变量名“ProgramData”是的,我在发布前试用过。但是当我远程执行时,
ProgramData
环境变量不存在
Get WMIObject-Class Win32\u Environment-ComputerName…
$specialFolder = Get-RemoteSpecialFolder "MyRemoteMachine" "Common AppData"