如果我的服务启动或停止,我有这个代码运行PowerShell脚本.
Timer timer1 = new Timer();
ServiceController sc = new ServiceController("MyService");
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer1.Interval = 10000;
timer1.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
if ((sc.Status == ServiceControllerStatus.StartPending) || (sc.Status == ServiceControllerStatus.Stopped))
{
StartPs();
}
}
private void StartPs()
{
PSCommand cmd = new PSCommand();
cmd.AddScript(@"C:\windows\security\dard\StSvc.ps1");
PowerShell posh = PowerShell.Create();
posh.Commands = cmd;
posh.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
当我从cmd提示符中删除我的服务时工作正常但是即使我的服务启动并运行,powershell脚本也会继续自行执行(它在计算机上附加一个文件)任何想法为什么?
我回到我的Active Directory工具......
我试图在用户的"成员"属性中列出组.以下是我使用的功能:
public static DataTable ListGroupsByUser(string selectedOu)
{
DataTable groupListByUser = new DataTable();
String dom = "OU=" + selectedOu + ",OU=XXX,DC=XXX,DCXXX,DC=XXX,DC=XXX";
DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + dom);
DataColumn column;
DataRow row;
column = new DataColumn();
column.ColumnName = "ID";
groupListByUser.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "User";
groupListByUser.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Groups";
groupListByUser.Columns.Add(column);
int i = 1;
foreach (DirectoryEntry child in directoryObject.Children)
{
row = groupListByUser.NewRow();
groupListByUser.Rows.Add(row);
row["ID"] = i++;
if (child.Properties["memberOf"].Value != null)
{
row["User"] …Run Code Online (Sandbox Code Playgroud)