C#, Authenticate to WSUS
-
Hey all - I'm a total newb, and I'm trying to use the WSUS API to connect to a specified WSUS Server and "do stuff". The problem is that it currently uses the currently logged on use to connect to the WSUS server, which fails. If I do a "run as" on the program, and use the correct credentials, it works. So, the question is how to prompt for credentials (command line) and connect to the WSUS server?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.UpdateServices;
using Microsoft.UpdateServices.Administration;namespace WSUS_Check
{
class Program
{
static void Main(string[] args)
{
//--- Vars ---//
string Server;
int Port, ans, count;
bool Secure;
ArrayList TGIDs;//--- Header ---// System.Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); System.Console.WriteLine("Custom WSUS Check, v0.01"); System.Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); System.Console.WriteLine(); //--- Get connection information ---// System.Console.Write("WSUS Server: "); Server = System.Console.ReadLine(); System.Console.Write("Port: "); Port = int.Parse(System.Console.ReadLine()); do { System.Console.Write("Secure \[1\\\\0\]: "); ans = int.Parse(System.Console.ReadLine()); } while ( (ans != 1) && (ans != 0) ); if (ans == 1) { Secure = true; } else { Secure = false; } //--- Connect to the specified WSUS Server ---// IUpdateServer Host; try { Host = AdminProxy.GetUpdateServer(Server, Secure, Port); //--- Working Section for Auth ---// //--- End working section for Auth ---// System.Console.WriteLine("Connected to " + Host.Name.ToString() + " \[WSUS v. " + Host.Version.ToString() + "\]"); System.Console.WriteLine(); //--- Enumerate Groups ---// S
-
Hey all - I'm a total newb, and I'm trying to use the WSUS API to connect to a specified WSUS Server and "do stuff". The problem is that it currently uses the currently logged on use to connect to the WSUS server, which fails. If I do a "run as" on the program, and use the correct credentials, it works. So, the question is how to prompt for credentials (command line) and connect to the WSUS server?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.UpdateServices;
using Microsoft.UpdateServices.Administration;namespace WSUS_Check
{
class Program
{
static void Main(string[] args)
{
//--- Vars ---//
string Server;
int Port, ans, count;
bool Secure;
ArrayList TGIDs;//--- Header ---// System.Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); System.Console.WriteLine("Custom WSUS Check, v0.01"); System.Console.WriteLine("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); System.Console.WriteLine(); //--- Get connection information ---// System.Console.Write("WSUS Server: "); Server = System.Console.ReadLine(); System.Console.Write("Port: "); Port = int.Parse(System.Console.ReadLine()); do { System.Console.Write("Secure \[1\\\\0\]: "); ans = int.Parse(System.Console.ReadLine()); } while ( (ans != 1) && (ans != 0) ); if (ans == 1) { Secure = true; } else { Secure = false; } //--- Connect to the specified WSUS Server ---// IUpdateServer Host; try { Host = AdminProxy.GetUpdateServer(Server, Secure, Port); //--- Working Section for Auth ---// //--- End working section for Auth ---// System.Console.WriteLine("Connected to " + Host.Name.ToString() + " \[WSUS v. " + Host.Version.ToString() + "\]"); System.Console.WriteLine(); //--- Enumerate Groups ---// S
Since this is a console app, you could just create a batch file to run your program as who ever with the runas command. That way you don't need to have have to modify your code.
Just because we can; does not mean we should.
-
Since this is a console app, you could just create a batch file to run your program as who ever with the runas command. That way you don't need to have have to modify your code.
Just because we can; does not mean we should.
-
Sure, that's one way, but not the way I'd like to see it done. The code has to be modified anyways, midas well do it now. Plus I can learn the an auth. peice from this, which will be cool.