Package org.metricshub.winrm
Class WinRMClient
java.lang.Object
org.metricshub.winrm.WinRMClient
- All Implemented Interfaces:
AutoCloseable
The fluent entry point of the library: a reusable WinRM connection to one host, created with
a builder and closed with try-with-resources. One client authenticates once and can run any
number of WQL queries and commands over the same connection.
try (
WinRMClient client = WinRMClient.builder("server01.acme.com")
.credentials("ACME\\admin", password)
.timeout(Duration.ofSeconds(30))
.build()) {
WqlResult services = client.wql("SELECT Name, State FROM Win32_Service").execute();
for (WqlRow row : services) {
System.out.println(row.string("Name") + " is " + row.string("State"));
}
CommandResult result = client.command("ipconfig /all").execute();
System.out.println(result.stdout());
}
Besides the blocking execute() terminals, both operations can stream:
WqlRequest.stream() yields WQL rows lazily page by page, and
CommandRequest.start() returns a RemoteProcess whose output is consumed while
the command is still running.
Thread-safety: a client may be shared between threads, but a WinRM connection is a serial channel — concurrent operations are executed one at a time, and an open stream or process holds the connection until it is closed.
Failures are reported through the unchecked
WinRMClientException hierarchy; the legacy static
helpers (WinRMWqlExecutor,
WinRMCommandExecutor) and their checked exceptions are
unaffected.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder ofWinRMClientinstances: connection-scoped settings with sensible defaults. -
Method Summary
Modifier and TypeMethodDescriptionstatic WinRMClient.BuilderStart building a client for the given host.voidclose()Close the client and release its connection.Prepare a command execution.hostname()Get the hostname this client connects to.voiduploadFile(Path localFile, String remoteFile) Copy a local file to an explicit path on the remote host, through the WinRM connection itself (no SMB, no extra port).Prepare a WQL query.
-
Method Details
-
builder
Start building a client for the given host.- Parameters:
hostname- the host to connect to (mandatory; for Kerberos, use the FQDN the KDC knows)- Returns:
- a new
WinRMClient.Builder
-
wql
Prepare a WQL query. Nothing is sent untilWqlRequest.execute()is called.- Parameters:
query- the WQL query, e.g.SELECT Name, State FROM Win32_Service- Returns:
- the request, to configure and execute
-
command
Prepare a command execution. Nothing is sent untilCommandRequest.execute()is called.- Parameters:
commandLine- the command line to execute (run throughcmd.exeby the remote shell)- Returns:
- the request, to configure and execute
-
uploadFile
Copy a local file to an explicit path on the remote host, through the WinRM connection itself (no SMB, no extra port). The transfer is digest-verified and skipped when the destination already has identical content; the destination directory is created when needed. The client's timeout applies.- Parameters:
localFile- the local file to copyremoteFile- the absolute destination path on the remote host, e.g.C:\Windows\Temp\collect.ps1- Throws:
WinRMTimeoutException- when the timeout elapses firstWinRMClientException- for any other failure
-
hostname
Get the hostname this client connects to.- Returns:
- the hostname
-
close
public void close()Close the client and release its connection. Idempotent; operations attempted after closing throwIllegalStateException.- Specified by:
closein interfaceAutoCloseable
-