Bagikan melalui


Pembuatan skrip di Manajemen Jarak Jauh Windows

Scripting API di WinRM dan COM API yang menyertainya untuk C++ dirancang untuk mencerminkan operasi protokol WS-Management.

API Scripting WinRM di Windows Remote Management mendukung semua operasi protokol WS-Management kecuali satu. Ini tidak mengizinkan langganan ke peristiwa. Untuk berlangganan peristiwa dari Log Peristiwa Sistem BMC, Anda harus menggunakan alat baris perintah Wecutil atau Wevtutil. Untuk informasi selengkapnya, lihat Peristiwa.

API Scripting WinRM dipanggil oleh Winrm.vbs, alat baris perintah, yang ditulis dalam Visual Basic Scripting Edition (VBScript). Winrm.vbs menyediakan contoh cara menggunakan Api Pembuatan Skrip WinRM.

Menggunakan WSman Dibandingkan dengan Menggunakan Scripting WMI

WMI terhubung ke komputer jarak jauh melalui DCOM, yang memerlukan konfigurasi yang dijelaskan dalam Menyambungkan ke WMI pada Komputer Jarak Jauh. WinRM tidak menggunakan DCOM untuk menyambungkan ke komputer jarak jauh. Sebaliknya, protokol WS-Management mengirim pesan SOAP dan layanan menggunakan satu port untuk HTTP dan port untuk transportasi HTTPS.

Tidak seperti alat baris perintah winrm , skrip harus menyediakan XML yang diperlukan untuk meneruskan ke pesan protokol WS-Management. Mereka juga harus menyediakan URI. Untuk informasi selengkapnya, lihat URI Sumber Daya dan Manajemen Jarak Jauh Windows dan WMI.

WMI Scripting API berfungsi dengan objek, seperti instans Win32_LogicalDisk, yang mewakili sumber daya di komputer. Kelas WMI ini didefinisikan dalam file Managed Object Format (MOF), yang disimpan dalam bentuk biner di repositori WMI. Di WMI, operasi Dapatkan untuk satu sumber daya atau kueri untuk beberapa instans mengembalikan objek WMI.

Skrip WinRM tidak mengembalikan objek, melainkan aliran teks XML. Untuk informasi selengkapnya, lihat Manajemen Jarak Jauh Windows dan WMI.

Menampilkan Output XML dari Skrip WinRM

API Skrip WinRM mendapatkan dan menerima string XML yang menjelaskan sumber daya. XML yang dihasilkan dalam bentuk aliran teks dan memerlukan transformasi XML untuk ditampilkan dengan cara lain.

Skrip WinRM berikut menghasilkan output XML mentah.

Set Wsman = CreateObject("Wsman.Automation")
Set xmlFile = CreateObject( "MSxml.DOMDocument")
Set Session = Wsman.CreateSession
Response = Session.Get("http://schemas.microsoft.com/wbem/wsman/" _
    & "1/wmi/root/cimv2/Win32_Service?Name=Spooler")
xmlFile.LoadXml(Response)
xmlFile.Save( "c:\RawOutput.xml")

Blok teks berikut menunjukkan output XML dari skrip WinRM.

<p:Win32_Service xmlns:xsi="https://www.w3.org/2001/XMLSchema-
instance" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1
/wmi/root/cimv2/Win32_Service" xmlns:cim="https://schemas.dmtf
.org/wbem/wsman/1/base" cim:Class="Win32_Service"><p:AcceptP
ause>false</p:AcceptPause><p:AcceptStop>true</p:AcceptStop>
<p:Caption>Print Spooler</p:Caption><p:CheckPoint>0</p:CheckP
oint><p:CreationClassName>Win32_Service</p:CreationClassName>
<p:Description>Loads files to memory for later printing</p:De
scription><p:DesktopInteract>true</p:DesktopInteract><p:Displ
ayName>Print Spooler</p:DisplayName><p:ErrorControl>Normal</p
:ErrorControl><p:ExitCode>0</p:ExitCode><p:InstallDate xsi:ni
l="true"/><p:Name>spooler</p:Name><p:PathName>C:\Windows\Syst
em32\spoolsv.exe</p:PathName><p:ProcessId>1720</p:ProcessId><
p:ServiceSpecificExitCode>0</p:ServiceSpecificExitCode><p:Ser
viceType>Own Process</p:ServiceType><p:Started>true</p:Starte
d><p:StartMode>Auto</p:StartMode><p:StartName>LocalSystem</p:
StartName><p:State>Running</p:State><p:Status>OK</p:Status><p
:SystemCreationClassName>Win32_ComputerSystem</p:SystemCreati
onClassName><p:SystemName>wsplab6-4</p:SystemName><p:TagId>0<
/p:TagId><p:WaitHint>0</p:WaitHint><cim:Location xmlns:cim="h
ttp://schemas.dmtf.org/wbem/wsman/1/base" xmlns:a="https://sc
hemas.xmlsoap.org/ws/2004/08/addressing" xmlns:w="https://sche
mas.dmtf.org/wbem/wsman/1/wsman"><a:Address>https://schemas.xm
lsoap.org/ws/2004/08/addressing/role/anonymous</a:Address><a:
ReferenceParameters><w:ResourceURI>https://schemas.microsoft.c
om/wbem/wsman/1/wmi/root/cimv2/Win32_Service</w:ResourceURI><
w:SelectorSet><w:Selector Name="Name">spooler</w:Selector></w
:SelectorSet></a:ReferenceParameters></cim:Location></p:Win32
_Service>

Skrip Anda dapat menggunakan transformasi XML untuk membuat output ini lebih mudah dibaca. Untuk informasi selengkapnya, lihat Menampilkan Output XML dari Skrip WinRM.

Versi skrip berikut memformat XML ke dalam output yang dapat dibaca manusia.

Set Wsman = CreateObject("Wsman.Automation")
Set xmlFile = CreateObject( "MSXml.DOMDocument" )
Set xslFile = CreateObject( "MSXml.DOMDocument" )

Set Session = Wsman.CreateSession
Response = Session.Get("http://schemas.microsoft.com/wbem/wsman/" _
    & "1/wmi/root/cimv2/Win32_Service?Name=Spooler")
xmlFile.LoadXml(Response)
xslFile.Load( "WsmTxt.xsl" )
Wscript.Echo xmlFile.TransformNode( xslFile )

Transformasi XSL membuat output berikut.

Win32_Service
    AcceptPause = false
    AcceptStop = true
    Caption = Print Spooler
    CheckPoint = 0
    CreationClassName = Win32_Service
    Description = Loads files to memory for later printing
    DesktopInteract = true
    DisplayName = Print Spooler
    ErrorControl = Normal
    ExitCode = 0
    InstallDate = null
    Name = Spooler
    PathName = C:\Windows\System32\spoolsv.exe
    ProcessId = 1720
    ServiceSpecificExitCode = 0
    ServiceType = Own Process
    Started = true
    StartMode = Auto
    StartName = LocalSystem
    State = Running
    Status = OK
    SystemCreationClassName = Win32_ComputerSystem
    SystemName = wsplab6-4
    TagId = 0
    WaitHint = 0

Skrip WinRM dan Output Winrm.cmd

Output dari skrip WinRM dikodekan dalam Unicode. Jika Anda membuat FileSystemObject dan menulis file dari skrip, file yang dihasilkan adalah Unicode. Namun, jika Anda mengalihkan output ke file, pengodeannya adalah ANSI. Jika Anda mengalihkan output ke file XML dan ada karakter Unicode dalam output, XML tidak akan valid. Ketahuilah bahwa alat baris perintah Winrm menghasilkan ANSI.

Tentang Manajemen Jarak Jauh Windows

Menggunakan Manajemen Jarak Jauh Windows

MSXSL

Referensi DOM