diff --git a/Program.cs b/Program.cs index 6220446..645bc89 100644 --- a/Program.cs +++ b/Program.cs @@ -97,8 +97,9 @@ static void Main(string[] args) execCommand.Add(new Option(new[] { "--site-code", "-sc" }, "The three character site code (e.g., \"PS1\") (default: the site code of the client running SharpSCCM)")); execCommand.Add(new Option(new[] { "--sms-provider", "-sms" }, "The IP address, FQDN, or NetBIOS name of the SMS Provider to connect to (default: the current management point of the client running SharpSCCM)")); execCommand.Add(new Option(new[] { "--wait-time", "-w" }, () => 300, "The time (in seconds) to wait for the deployment to execute before cleaning up (default: 300)")); + execCommand.Add(new Option(new[] { "--working-dir", "-dir" }, "The working directory to execute a command, binary, or script from")); execCommand.Handler = CommandHandler.Create( - (string device, string collectionId, string collectionName, string path, string relayServer, string resourceId, bool runAsSystem, string collectionType, string user, int waitTime, string smsProvider, string siteCode) => + (string device, string collectionId, string collectionName, string path, string workingDir, string relayServer, string resourceId, bool runAsSystem, string collectionType, string user, int waitTime, string smsProvider, string siteCode) => { if (!string.IsNullOrEmpty(relayServer) && !string.IsNullOrEmpty(path) || (string.IsNullOrEmpty(relayServer) && string.IsNullOrEmpty(path))) { @@ -117,7 +118,7 @@ static void Main(string[] args) ManagementScope wmiConnection = MgmtUtil.NewWmiConnection(smsProvider, null, siteCode); if (wmiConnection != null && wmiConnection.IsConnected) { - SmsProviderWmi.Exec(wmiConnection, collectionId, collectionName, device, path, relayServer, resourceId, !runAsSystem, collectionType, user, waitTime); + SmsProviderWmi.Exec(wmiConnection, collectionId, collectionName, device, path, workingDir, relayServer, resourceId, !runAsSystem, collectionType, user, waitTime); } } }); @@ -664,6 +665,7 @@ static void Main(string[] args) // invoke var invokeCommand = new Command("invoke", "A group of commands that execute actions on an SMS Provider"); + invokeCommand.AddGlobalOption(new Option(new[] { "--management-point", "-mp" }, "The IP address, FQDN, or NetBIOS name of the management point to connect to (default: the current management point of the client running SharpSCCM)")); invokeCommand.AddGlobalOption(new Option(new[] { "--sms-provider", "-sms" }, "The IP address, FQDN, or NetBIOS name of the SMS Provider to connect to (default: the current management point of the client running SharpSCCM)")); invokeCommand.AddGlobalOption(new Option(new[] { "--site-code", "-sc" }, "The three character site code (e.g., \"PS1\") (default: the site code of the client running SharpSCCM)")); rootCommand.Add(invokeCommand); @@ -743,21 +745,21 @@ static void Main(string[] args) invokeClientPush.Add(new Option(new[] { "--client-id", "-i" }, "The SMS client GUID to use that corresponds to a previously registered device and certificate")); invokeClientPush.Add(new Option(new[] { "--target", "-t" }, "The NetBIOS name, IP address, or if WebClient is enabled on the site server, the IP address and port (e.g., \"192.168.1.1@8080\") of the relay/capture server (default: the machine running SharpSCCM)")); invokeClientPush.Handler = CommandHandler.Create( - (string smsProvider, string siteCode, bool asAdmin, string certificate, string clientId, string target) => + (string managementPoint, string smsProvider, string siteCode, bool asAdmin, string certificate, string clientId, string target) => { - if (smsProvider == null || siteCode == null) + if (managementPoint == null || siteCode == null) { - (smsProvider, siteCode) = ClientWmi.GetCurrentManagementPointAndSiteCode(); + (managementPoint, siteCode) = ClientWmi.GetCurrentManagementPointAndSiteCode(); } - if (!string.IsNullOrEmpty(smsProvider) && !string.IsNullOrEmpty(siteCode)) + if (!string.IsNullOrEmpty(managementPoint) && !string.IsNullOrEmpty(siteCode)) { if (!asAdmin) { // Use certificate of existing device if provided if (!string.IsNullOrEmpty(certificate) && !string.IsNullOrEmpty(clientId)) { - (MessageCertificateX509 signingCertificate, _, SmsClientId smsClientId) = MgmtPointMessaging.GetCertsAndClientId(smsProvider, siteCode, certificate, clientId); - MgmtPointMessaging.SendDDR(signingCertificate, target, smsProvider, siteCode, smsClientId); + (MessageCertificateX509 signingCertificate, _, SmsClientId smsClientId) = MgmtPointMessaging.GetCertsAndClientId(managementPoint, siteCode, certificate, clientId); + MgmtPointMessaging.SendDDR(signingCertificate, target, managementPoint, siteCode, smsClientId); } else if (!string.IsNullOrEmpty(certificate) && string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(certificate) && !string.IsNullOrEmpty(clientId)) { @@ -767,14 +769,18 @@ static void Main(string[] args) else { MessageCertificateX509 signingCertificate = MgmtPointMessaging.CreateUserCertificate(); - SmsClientId smsClientId = MgmtPointMessaging.RegisterClient(signingCertificate, target, smsProvider, siteCode); - MgmtPointMessaging.SendDDR(signingCertificate, target, smsProvider, siteCode, smsClientId); + SmsClientId smsClientId = MgmtPointMessaging.RegisterClient(signingCertificate, target, managementPoint, siteCode); + MgmtPointMessaging.SendDDR(signingCertificate, target, managementPoint, siteCode, smsClientId); } } else { if (!string.IsNullOrEmpty(target)) { + if (string.IsNullOrEmpty(smsProvider)) + { + smsProvider = managementPoint; + } SmsProviderWmi.GenerateCCR(target, smsProvider, siteCode); } else @@ -1059,13 +1065,14 @@ static void Main(string[] args) newApplication.Add(new Option(new[] { "--path", "-p" }, "The local or UNC path of the binary/script the application will execute (e.g., \"C:\\Windows\\System32\\calc.exe\", \"\\\\site-server.domain.com\\Sources$\\my.exe") { IsRequired = true }); newApplication.Add(new Option(new[] { "--run-as-user", "-r" }, "Execute the application in the context of the logged on user (default: SYSTEM)")); newApplication.Add(new Option(new[] { "--show", "-s" }, "Show the application in the Configuration Manager console (default: hidden)")); + newApplication.Add(new Option(new[] { "--working-dir", "-dir" }, "The working directory to execute a command, binary, or script from")); newApplication.Handler = CommandHandler.Create( - (string smsProvider, string siteCode, string name, string path, bool runAsUser, bool show) => + (string smsProvider, string siteCode, string name, string path, string workingDir, bool runAsUser, bool show) => { ManagementScope wmiConnection = MgmtUtil.NewWmiConnection(smsProvider, null, siteCode); if (wmiConnection != null && wmiConnection.IsConnected) { - SmsProviderWmi.NewApplication(wmiConnection, name, path, runAsUser, show); + SmsProviderWmi.NewApplication(wmiConnection, name, path, workingDir, runAsUser, show); } }); diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index ecac5e7..287d903 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -27,5 +27,5 @@ // Minor Version // Revision // -[assembly: AssemblyVersion("2.0.10")] -[assembly: AssemblyFileVersion("2.0.10")] +[assembly: AssemblyVersion("2.0.11")] +[assembly: AssemblyFileVersion("2.0.11")] diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b2c1546..065b211 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,10 @@ # SharpSCCM Release Notes +### Version 2.0.11 (July 3, 2024) +##### Changes +- Added working directory to exec command +- Added -mp back to invoke command + ### Version 2.0.10 (April 15, 2024) ##### Changes - Added get admins command to list SCCM admin users diff --git a/lib/SmsProviderWmi.cs b/lib/SmsProviderWmi.cs index 02ac95d..ab4cc9d 100644 --- a/lib/SmsProviderWmi.cs +++ b/lib/SmsProviderWmi.cs @@ -9,7 +9,7 @@ namespace SharpSCCM { public static class SmsProviderWmi { - public static void Exec(ManagementScope wmiConnection, string collectionId = null, string collectionName = null, string deviceName = null, string applicationPath = null, string relayServer = null, string resourceId = null, bool runAsUser = true, string collectionType = null, string userName = null, int waitTime = 300) + public static void Exec(ManagementScope wmiConnection, string collectionId = null, string collectionName = null, string deviceName = null, string applicationPath = null, string workingDir = null, string relayServer = null, string resourceId = null, bool runAsUser = true, string collectionType = null, string userName = null, int waitTime = 300) { ManagementObject collection = null; if (!string.IsNullOrEmpty(collectionName) || !string.IsNullOrEmpty(collectionId)) @@ -37,7 +37,7 @@ public static void Exec(ManagementScope wmiConnection, string collectionId = nul string newDeploymentName = $"{newApplicationName}_{(string)collection["CollectionID"]}_Install"; applicationPath = !string.IsNullOrEmpty(relayServer) ? $"\\\\{relayServer}\\C$" : applicationPath; // Hide from ConfigMgr Console by default - NewApplication(wmiConnection, newApplicationName, applicationPath, runAsUser, false); + NewApplication(wmiConnection, newApplicationName, applicationPath, workingDir, runAsUser, false); NewDeployment(wmiConnection, newApplicationName, null, (string)collection["CollectionID"]); Console.WriteLine("[+] Waiting for new deployment to become available..."); bool deploymentAvailable = false; @@ -722,7 +722,7 @@ public static void UpdateUserPolicyForDevice(ManagementScope wmiConnection, stri Exec(wmiConnection, deviceName: deviceName, applicationPath: commandToExecute, runAsUser: false, collectionType: "device"); } - public static ManagementObject NewApplication(ManagementScope wmiConnection, string name, string path, bool runAsUser = false, bool show = false) + public static ManagementObject NewApplication(ManagementScope wmiConnection, string name, string path, string workingDir = null, bool runAsUser = false, bool show = false) { ManagementObject application = null; @@ -808,7 +808,7 @@ public static ManagementObject NewApplication(ManagementScope wmiConnection, str Script {path} - + {workingDir} {(runAsUser ? "User" : "System")} false