Skip to content

Commit

Permalink
Merge pull request #57 from Mayyhem/exec-working-dir
Browse files Browse the repository at this point in the history
Exec working dir
  • Loading branch information
Mayyhem authored Jul 3, 2024
2 parents ee50de4 + 533d982 commit d10ad1d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
31 changes: 19 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ static void Main(string[] args)
execCommand.Add(new Option<string>(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<string>(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<int>(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<string>(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)))
{
Expand All @@ -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);
}
}
});
Expand Down Expand Up @@ -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<string>(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<string>(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<string>(new[] { "--site-code", "-sc" }, "The three character site code (e.g., \"PS1\") (default: the site code of the client running SharpSCCM)"));
rootCommand.Add(invokeCommand);
Expand Down Expand Up @@ -743,21 +745,21 @@ static void Main(string[] args)
invokeClientPush.Add(new Option<string>(new[] { "--client-id", "-i" }, "The SMS client GUID to use that corresponds to a previously registered device and certificate"));
invokeClientPush.Add(new Option<string>(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))
{
Expand All @@ -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
Expand Down Expand Up @@ -1059,13 +1065,14 @@ static void Main(string[] args)
newApplication.Add(new Option<string>(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<bool>(new[] { "--run-as-user", "-r" }, "Execute the application in the context of the logged on user (default: SYSTEM)"));
newApplication.Add(new Option<bool>(new[] { "--show", "-s" }, "Show the application in the Configuration Manager console (default: hidden)"));
newApplication.Add(new Option<string>(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);
}
});

Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/SmsProviderWmi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -808,7 +808,7 @@ public static ManagementObject NewApplication(ManagementScope wmiConnection, str
<Provider>Script</Provider>
<Args>
<Arg Name=""InstallCommandLine"" Type=""String"">{path}</Arg>
<Arg Name=""WorkingDirectory"" Type=""String""/>
<Arg Name=""WorkingDirectory"" Type=""String"">{workingDir}</Arg>
<Arg Name=""ExecutionContext"" Type=""String"">{(runAsUser ? "User" : "System")}</Arg>
<Arg Name=""RequiresLogOn"" Type=""String""/>
<Arg Name=""RequiresElevatedRights"" Type=""Boolean"">false</Arg>
Expand Down

0 comments on commit d10ad1d

Please sign in to comment.