Это можно сделать с помощью P / Invoke;Ниже приведен пример.
#requires -version 2
Add-Type -MemberDefinition @"
[DllImport("wtsapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern void WTSFreeMemory(IntPtr pMemory);
[DllImport("wtsapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool WTSQueryUserConfig(
string pServerName,
string pUserName,
int WTSConfigClass,
out IntPtr ppBuffer,
out uint pBytesReturned);
[DllImport("wtsapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool WTSSetUserConfig(
string pServerName,
string pUserName,
int WTSConfigClass,
IntPtr pBuffer,
uint DataLength);
"@ -Namespace Win32Api -Name WtsApi32
$WTS_CONFIG_SHADOWING_SETTINGS = 14
function WTSQueryUserConfigShadowSettings {
[CmdletBinding()]
param(
[String] $computerName,
[String] $userName
)
$pBuffer = [IntPtr]::Zero
$bytesReturned = 0
$success = [Win32Api.WtsApi32]::WTSQueryUserConfig(
$computerName, # pServerName
$userName, # pUserName
$WTS_CONFIG_SHADOWING_SETTINGS, # WTSConfigClass
[Ref] $pBuffer, # ppBuffer
[Ref] $bytesReturned # pBytesReturned
)
if ( $success ) {
[Runtime.InteropServices.Marshal]::ReadInt32($pBuffer)
[Win32Api.WtsApi32]::WTSFreeMemory($pBuffer)
}
else {
$exception = New-Object ComponentModel.Win32Exception ([Runtime.InteropServices.Marshal]::GetLastWin32Error())
Write-Error -Exception $exception
}
}
function WTSSetUserConfigShadowSettings {
[CmdletBinding()]
param(
[String] $computerName,
[String] $userName,
[Int] [ValidateRange(0,4)] $shadowSettings
)
$pNewValue = [Runtime.InteropServices.Marshal]::AllocHGlobal([Runtime.InteropServices.Marshal]::SizeOf([Type] [Int]))
[Runtime.InteropServices.Marshal]::WriteInt32($pNewValue, $shadowSettings)
$dataLength = [Runtime.InteropServices.Marshal]::SizeOf($pNewValue)
$success = [Win32Api.WtsApi32]::WTSSetUserConfig(
$computerName, # pServerName
$userName, # pUserName
$WTS_CONFIG_SHADOWING_SETTINGS, # WTSConfigClass
$pNewValue, # pBuffer
$dataLength # DataLength
)
if ( $success ) {
[Runtime.InteropServices.Marshal]::FreeHGlobal($pNewValue)
}
else {
$exception = New-Object ComponentModel.Win32Exception ([Runtime.InteropServices.Marshal]::GetLastWin32Error())
Write-Error -Exception $exception
}
}
function Get-RDShadowingSetting {
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[String[]] [ValidateNotNullOrEmpty()] $UserName,
[Parameter(ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
[String[]] $ComputerName = [Net.Dns]::GetHostName()
)
process {
foreach ( $computerNameItem in $ComputerName ) {
foreach ( $userNameItem in $userName ) {
New-Object PSObject -Property @{
"ComputerName" = $computerNameItem
"UserName" = $userNameItem
"RDShadowingSetting" = WTSQueryUserConfigShadowSettings $computerNameItem $userNameItem
} | Select-Object ComputerName,UserName,RDShadowingSetting
}
}
}
}
function Set-RDShadowingSetting {
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[String[]] [ValidateNotNullOrEmpty()] $UserName,
[Parameter(Position = 1)]
[Int] [ValidateRange(0,4)] $RDShadowingSetting,
[Parameter(ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
[String[]] $ComputerName = [Net.Dns]::GetHostName()
)
process {
foreach ( $computerNameItem in $ComputerName ) {
foreach ( $userNameItem in $userName ) {
WTSSetUserConfigShadowSettings $computerNameItem $userNameItem $RDShadowingSetting
}
}
}
}
Функция Get-RDShadowingSetting
возвращает значение 0
- 4
в свойстве RDShadowingSetting
, соответствующее настройке, которую вы хотите использовать:
Value Meaning
----- -------
0 Disable remote control
1 Enabled/require user's permission/interact with the session
2 Enabled/don't require user's permission/interact with the session
3 Enabled/require user's permission/view the user's session
4 Enabled/don't require user's permission/view the user's session
Функция Set-RDShadowingSetting
позволяет обновлять значение для пользователя;Например:
Set-RDShadowingSetting "KenDyer" 0
Это отключило бы дистанционное управление для учетной записи KenDyer
на локальном компьютере.
Функции WTSQueryUserConfigShadowSettings
и WTSSetUserConfigShadowSettings
- это функции, которые выполняют фактическую WindowsВызовы API.
(Получение списка имен компьютеров или пользователей оставлено читателю в качестве упражнения.)
Ссылка на документацию API: https://msdn.microsoft.com/en-us/library/aa383859.aspx