‏הצגת רשומות עם תוויות Powershell. הצג את כל הרשומות
‏הצגת רשומות עם תוויות Powershell. הצג את כל הרשומות

יום שלישי, 29 באוקטובר 2013

How to add user to local administrator group of remote computer



First we will take our input.
We need domain in order to search for the user in the Active Directory the remote computer we want to add the user as admin into and the username of the user who want to be the admin.



Now we want to get inside the local Administrator group inside the remote computer.
In order to do it we will use the [ADSI] adapter.
Explanation:
ADSI stands for Active Directory® Services Interface. Despite the name, ADSI isn’t actually specific to Active Directory.
ADSI can connect to a variety of directory services, including Windows NT®-style directories—not just Windows NT domains, mind you, but also to the local Security Accounts Manager (SAM) on standalone and member computers. 
As the name implies, a type adapter "adapts" a .NET Framework object type into a more consistent, and sometimes simpler, format.
For more information you can visit the great post of Don Jones at:
We will use this adapter in order to access the local administrator group in the remote computer:



After we have the group we will add the user:






יום שני, 21 באוקטובר 2013

Export group members in AD to CSV file

When you receive task for getting members of a group it can be simply done if the group has 5-10 members.
But sometimes groups have more than 20 users and it can be frustrated.
You can use the following PowerShell commands to export the users:
import-module activedirectory #loading the AD module
get-ADGroupmember groupName



















If we want to see only the names we can use the pipeline: select name









We can also create script that will do it automatically by giving the name of the group and create CSV file with the names to desktop:



















Code can be found here: http://pastie.org/8418008#



יום שבת, 19 באוקטובר 2013

How to call powershell script with C++ code

So you wrote powerShell script and you want to run the script.
You double click the script (extension .ps1) and what happened?
You see your script text, yeah, not what you thought, ah ?

So, in powershell, if you want to run your script, just press right click on the script and then "Run with PowerShell":















But maybe we are not the kind of guy that likes the right click. 
We are the "double click" guy.
So we can do it simply by writing C++ program:


#include<iostream>
#include <io.h>   // For access().
#include <sys/types.h>  // For stat().
#include <sys/stat.h>   // For stat().
#include <string>
using namespace std;


void main()
{
       string strPath = "d:\\callPowerShell.ps1";
//access function:
       //The function returns 0 if the file has the given mode.
       //The function returns –1 if the named file does not exist or does not have the given mode
       if(access(strPath.c_str(),0) == 0)
       {
             
              system("start powershell.exe Set-ExecutionPolicy RemoteSigned \n");
              system("start powershell.exe d:\\callPowerShell.ps1");
              system("cls");
       }
       else
       {
              system("cls");
              cout << "File is not exist";
              system("pause");
       }
}



Explanation:
·   We are using the access function to check if the path exist:
int _access(const char *path, int mode);
We define the mode with 0 to check if the path exist.
If the function returns 0, it means the given mode (in our case the mode that checks if the path exists) is correct.
For more information on Microsoft MSDN:
http://msdn.microsoft.com/en-us/library/1w06ktdy.aspx

·        Regarding c_str()
    Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
      This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('\0') at the end.

       So you could also write:

char arr[22] = {"d:\\callPowerShell.ps1"};
       ...
if(access(arr,0) == 0)
...
For more information:
http://www.cplusplus.com/reference/string/string/c_str/


Our powerShell script's code (callPowerShell.ps1) is:
get-process
read-host


So we will run the exe file and receive: