Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (9/25/09)

Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (9/25/09)

  • Comments 2
  • Likes
Bookmark and Share

Can I Find Out Who Has Access to a Group of Files and Folders?

Hey, Scripting Guy! Question

Hey, Scripting Guy!  For security reasons I need to find out who has access to a certain group of files and folders. Using the Get-Acl cmdlet, I can get a list of which IDs and groups have which kinds of access to the target files and folders. I would also like to list the share permissions of the shared folders by ID and group.

-- AC

 

Hey, Scripting Guy! Answer

Hello AC,

You can do this by utilizing the Win32_LogicalShareSecuritySetting WMI class.  But that WMI class is a bit nasty to use, even from within Windows PowerShell.  I would use Get-Acl Windows PowerShell cmdlet, and give it the UNC to the share you are interested in working with. This is seen here.

Get-Acl –path file://computername/sharename


 

How Can I See Which Parameters a Cmdlet Can Take? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! First of all let me tell you how much I appreciate the power that you guys gave to Windows Powershell. It's amazing! Now let me come to the point. How can I find out the parameters that a particular cmdlet can take? For example, the Get-Date cmdlet can use the –uformat parameter as seen here:

Get-date –uformat %j

How do I find out that the Get-Date cmdlet is able to accept a parameter called uformat? When I use the Get-Member cmdlet, it only lists methods and properties but not argument. Furthermore, after I know that the Get-Date cmdlet will accept the uformat parameter, how do I know what values I am able to supply for the parameter (%j in this example)?

-- PD

 

Hey, Scripting Guy! Answer

Hello PD,

You need to learn how to use the Get-Help cmdlet. There are several switches you can use with the Get-Help cmdlet, but the one you are interested in is the one that will display the full Help information. To find the complete Help information for the Get-Date cmdlet, you can use the syntax seen here:

Get-Help Get-Date –Full

When this command runs, it will list all of the information related to each of the parameters. This is a decent amount of detailed information, and as a result you may wish to pipe the results to the more function so that the information is displayed one page at a time. This is seen here:

Get-Help Get-Date –Full | More

To find out how to use the Get-Help cmdlet, you can use Get-Help. This is seen here:

Get-help Get-Help


 

How Can I Save the Output of a Script to a Text File?

Hey, Scripting Guy! Question

Hey Scripting Guy! In regards to the article about running Windows PowerShell scripts against remote computers, can you tell me how I can save the output of the following command to a text file?

( foreach ($i in Get-Content "C:\Scripts\Test.txt")
    {$i + "`n" + "=========================="; get-wmiobject win32_bios -computername $i} )

-- PF

Hey, Scripting Guy! Answer

Hello PF,

Inside the foreach loop, you can use the redirection  arrows (>>) to write to the text file as seen here. (By the way, the parentheses surrounding your command are not needed. In addition, the command is hard to read when squashed together onto a single line.  Because the semicolon is a line separator, I removed the semicolon from your command and moved everything to separate lines. This makes it easier to read, and easier to see what is going on with the code:
 

foreach ($i in Get-Content "C:\Scripts\Test.txt")
 {
   $i + "`n" + "==========================" >> c:\fso\mytext.txt
   get-wmiobject win32_bios -computername $i >> c:\fso\mytext.txt
 }

Or you can use the Out-File cmdlet as seen here:

foreach ($i in Get-Content "C:\Scripts\Test.txt")
 {
   $i + "`n" + "==========================" | Out-File –filepath c:\fso\mytext.txt –append
   get-wmiobject win32_bios -computername $i | Out-File –filepath c:\fso\mytext.txt –append
 }

You might be interested in the Windows PowerShell getting started page because it provides links to a number of resources that will assist you in getting started with Windows PowerShell. You may also want to pick up a good book on Windows PowerShell, such as the Microsoft Press book, Microsoft Windows PowerShell Step by Step, which has hands-on exercises that will assist you in learning Windows PowerShell. 


 
Can I Get a Computer Name and Computer Information Saved to a Text File?

Hey, Scripting Guy! Question

Hey Scripting Guy! When I execute the following query, I get the name of the computer displayed on the Windows PowerShell console:

foreach($i in get-content "D:\powershell\server.txt"){$i+"`n====================="; get-wmiobject win32_bios -computername $i| out-file "D:\powershell\output.txt" -append}

What I see displayed is shown here:

Image of what is displayed when script is run


When I open the text file, I can see the results of the WMI query, but there is no corresponding computer name information, as shown here:

Image of the text file's contents without computer name information

 

How can I get both the computer name and the output corresponding to that computer in the output file?

-- JG

 

Hey, Scripting Guy! Answer

Hello JG,

You have two different commands in your script. This is what is giving you two different results.  The semicolon is a command separator. You will need to modify your code so that you are writing to the text file at the end of each command, as seen here:

foreach($i in get-content "D:\dhull\powershell\server.txt")
{
$i+"`n=====================" | out-file "D:\powershell\output.txt" –append
get-wmiobject win32_bios -computername $i| out-file "D:\powershell\output.txt" –append
}


 

Troubleshooting a VBScript Error Related to Microsoft Access

Hey, Scripting Guy! Question

Hey Scripting Guy!  This is a VBScript problem that just about killed me while I was figuring out a solution to it. I figured I would send it to you, in case you can put it somewhere useful to save someone else the problem.

I have a fairly complex database export specification in an Access 2007 database, which I wanted to call from a scheduled task in VBScript. When I ran the script from within Access, VBScript kept giving me the error “Operation must use an updateable query” (code 800A0C01).

I spent ages before eventually realizing the TransferText method AcTextTransferType of  acExportDelim wasn’t translating properly to a constant and was defaulting to the ACTextTransferType acImportDelim. I tracked down the constant value (2) and substituted it and it worked. Here is the applicable portion of my script:

Set objaccess = CreateObject("Access.Application”)
db = objaccess.OpenCurrentDatabase("C:\MAPC_Documents\sam.accdb")
objaccess.docmd.TransferText acExportDelim, "samspec", "qryUploads", "C:\MAPC_Documents\sam.csv", 1
objaccess.docmd.Closedatabase

-- EM

 

Hey, Scripting Guy! Answer

Hello EM,

Thank you for sharing that information. You are right: Those different enumeration values can often cause problems.

 

 

How Can I Modify Windows Power Settings with VBScript? 

Hey, Scripting Guy! Question

Hey Scripting Guy! How can I modify Windows power settings with VBScript?  I would like to put this in a login or startup script, if I can.

-- EB

 

Hey, Scripting Guy! Answer

Hello EB,

You can script the Powercfg command-line utility. This article talks about running command-line utilities in VBScript. Here are the parameters the Powercfg utility will accept.

POWERCFG <command line options>
Description:
  This command line tool enables users to control the power settings
  on a system.

Parameter List:
  -LIST, -L   Lists all power schemes in the current user's environment.

              Usage: POWERCFG -LIST

  -QUERY, -Q  Displays the contents of the specified power scheme.

              Usage: POWERCFG -QUERY <SCHEME_GUID> <SUB_GUID>

              <SCHEME_GUID>  (optional) Specifies the GUID of the power scheme
                             to display, can be obtained by using powercfg -l.
              <SUB_GUID>     (optional) Specifies the GUID of the subgroup
                             to display.  Requires a SCHEME_GUID to be provided.

              If neither SCHEME_GUID or SUB_GUID are provided, the settings
              of the current user's active power scheme are displayed.
              If SUB_GUID is not specified, all settings in the specified
              power scheme are displayed.

  -CHANGE, -X Modifies a setting value in the current power scheme.

              Usage: POWERCFG -X <SETTING> <VALUE>

              <SETTING>   Specifies one of the following options:
                          -monitor-timeout-ac <minutes>
                          -monitor-timeout-dc <minutes>
                          -disk-timeout-ac <minutes>
                          -disk-timeout-dc <minutes>
                          -standby-timeout-ac <minutes>
                          -standby-timeout-dc <minutes>
                          -hibernate-timeout-ac <minutes>
                          -hibernate-timeout-dc <minutes>

              Example:
                  POWERCFG -Change -monitor-timeout-ac 5

              This would set the monitor idle timeout value to 5 minutes
              when on AC power.
-CHANGENAME Modifies the name of a power scheme and optionally it's
            description.

            Usage: POWERCFG -CHANGENAME <GUID> <name> <scheme description>

            If the description is omitted only the name will be changed.

-DUPLICATESCHEME
            Duplicates the specified power scheme.  The resulting
            GUID which represents the new scheme will be displayed.

            Usage: POWERCFG -DUPLICATESCHEME <GUID> <destination GUID>

            <GUID>  Specifies a scheme GUID obtained by using the powercfg -l.

            If <destination GUID> is omitted, a new GUID will be
            created for the duplicated scheme.

-DELETE, -D Deletes the power scheme with the specified GUID.

            Usage: POWERCFG -DELETE <GUID>

            <GUID>  obtained by using the LIST parameter.

-DELETESETTING
            Deletes a power setting.

            Usage: POWERCFG -DELETESETTING <SUB_GUID> <SETTING_GUID>

            <SUB_GUID>      Specifies the subgroup GUID.
            <SETTING_GUID>  Specifies the power setting guid.

-SETACTIVE, -S
            Makes the specified power scheme active on the system.

            Usage: POWERCFG -SETACTIVE <SCHEME_GUID>

            <SCHEME_GUID>  Specifies the scheme guid.

-GETACTIVESCHEME
            Retrieve the currently active power scheme.

            Usage: POWERCFG –GETACTIVESCHEME
-SETACVALUEINDEX
            Sets a value associated with a specified power setting
            while the system is powered by AC power.

            Usage: POWERCFG -SETACVALUEINDEX <SCHEME_GUID> <SUB_GUID>
                                             <SETTING_GUID> <SettingIndex>

            <SCHEME_GUID>   Specifies a power scheme GUID and may be
                            obtained by using PowerCfg /L.
            <SUB_GUID>      Specifies a subgroup of power setting GUID and may
                            be obtained by using "PowerCfg /Q."
            <SETTING_GUID>  Specifies an individual power setting GUID and may
                            be obtained by using "PowerCfg /Q".
            <SettingIndex>  Specifies which of the list of of possible values
                            this power setting will be set to.

            Example:
                POWERCFG -SetAcValueIndex <GUID> <GUID> <GUID> 5
                This would set the power setting's AC value to the 5th entry
                in the list of possible values for this power setting.
-SETDCVALUEINDEX
            Sets a value associated with a specified power setting
            while the system is powered by DC power.

            Usage: POWERCFG -SETDCVALUEINDEX <SCHEME_GUID> <SUB_GUID>
                                             <SETTING_GUID> <SettingIndex>
            <SCHEME_GUID>   Specifies a power scheme GUID and may be
                            obtained by using PowerCfg /L.
            <SUB_GUID>      Specifies a subgroup of power setting GUID and may
                            be obtained by using "PowerCfg /Q."
            <SETTING_GUID>  Specifies an individual power setting GUID and may
                            be obtained by using "PowerCfg /Q".
            <SettingIndex>  Specifies which of the list of possible values
                            this setting will be set to.

            Example:
                POWERCFG -SetDcValueIndex <GUID> <GUID> <GUID> 5
                This would set the power setting's DC value to the 5th entry
                in the list of possible values for this power setting.

-HIBERNATE, -H  <ON|OFF>
            Enables-Disables the hibernate feature.  Hibernate timeout is not
            supported on all systems.

             Usage: POWERCFG -H <ON|OFF>

 -AVAILABLESLEEPSTATES, -A
             Reports the sleep states available on the system
             Attempts to report reasons why sleep states are unavailable.

 -DEVICEQUERY
             Return a list of devices that meet the specified criteria.

             Usage: POWERCFG -DEVICEQUERY <queryflags>

             <queryflags>  Secifies one of the following criteria:

             wake_from_S1_supported  Return all devices that support waking the
                                     system from a light sleep state.
             wake_from_S2_supported  Return all devices that support waking the
                                     system from a deeper sleep state.
             wake_from_S3_supported  Return all devices that support waking the

                                     system from the deepest sleep state.
             wake_from_any           Return all devices that support waking the

                                     system from any sleep state.
             S1_supported            List devices supporting light sleep.
             S2_supported            List devices supporting deeper sleep.
             S3_supported            List devices supporting deepest sleep.
             S4_supported            List devices supporting hibernation.
             wake_programmable       List devices that are user-configurable
                                     to wake the system from a sleep state.
             wake_armed              List devices that are currently configured
                                     to wake the system from any sleep state.
             all_devices             Return all devices present in the system.
             all_devices_verbose     Return verbose list of devices.
             Example:
                 POWERCFG -DEVICEQUERY wake_armed

 -DEVICEENABLEWAKE
             Enable the device to wake the system from a sleep state.

             Usage: POWERCFG -DEVICEENABLEWAKE <devicename>

             <devicename>  Specifies a device retrieved using
                           "PowerCfg -DEVICEQUERY wake_programmable".
             Example:
                 POWERCFG -DEVICEENABLEWAKE "Microsoft USB IntelliMouse Explore
"

 -DEVICEDISABLEWAKE <devicename> disable the device from waking the system
             Disable the device from waking the system from a sleep state

             Usage: POWERCFG -DEVICEDISABLEWAKE

             <devicename>  Specifies a device retrieved using
                           "PowerCfg -DEVICEQUERY wake_armed".

 -IMPORT     Imports all power settings from the specified file.

             Usage: POWERCFG -IMPORT <filename> <GUID>

             <filename>  Specifiy a fully-qualified path to a file generated by
                         using "PowerCfg -EXPORT parameter".
             <GUID>      (optional) The settings are loaded into a power scheme
                         represented by this GUID. If not supplied, powercfg
                         will generate and use a new GUID

             Example:
                 POWERCFG -IMPORT c:\scheme.pow
 -EXPORT     Exports power scheme, represented by the specified GUID, to the
             specified file.

             Usage: POWERCFG -EXPORT <filename> <GUID>

             <filename>  Specify a fully-qualified path to a destination file.
             <GUID>      specifies a power scheme GUID and may be obtained by
                         using "PowerCfg /L"

             Example:
                 POWERCFG -EXPORT c:\scheme.pow 381b4222-f694-41f0-9685-ff5bb26
df2e
 -LASTWAKE   Reports information about what woke the system from the last
             sleep transition

 -HELP, -?   Displays information on command-line parameters.

 -ALIASES    Displays all aliases and their corresponding GUIDs.
             The user may use these aliases in place of any GUID on
              specified file.

              Usage: POWERCFG -EXPORT <filename> <GUID>

              <filename>  Specify a fully-qualified path to a destination file.
              <GUID>      specifies a power scheme GUID and may be obtained by
                          using "PowerCfg /L"

              Example:
                  POWERCFG -EXPORT c:\scheme.pow 381b4222-f694-41f0-9685-ff5bb26
0df2e
  -LASTWAKE   Reports information about what woke the system from the last
              sleep transition

  -HELP, -?   Displays information on command-line parameters.

  -ALIASES    Displays all aliases and their corresponding GUIDs.
              The user may use these aliases in place of any GUID on
              the commandline.

  -SETSECURITYDESCRIPTOR
              Sets a security descriptor associated with a specified
              power setting, power scheme, or action.

              Usage: POWERCFG -SETSECURITYDESCRIPTOR <GUID|ACTION> <SDDL>

              <GUID>       Specifies a power scheme or a power setting GUID.
              <ACTION>     Can be one of the following strings:
                           ActionSetActive, ActionCreate, ActionDefault
              <SDDL>       Specifies a valid security descriptor string in SDDL
                           format. Call POWERCFG -GETSECURITYDESCRIPTOR to see
                           an example SDDL STRING.

  -GETSECURITYDESCRIPTOR
              Gets a security descriptor associated with a specified
              power setting, power scheme, or action.

              Usage: POWERCFG -GETSECURITYDESCRIPTOR <GUID|ACTION>
              <GUID>       Specifies a power scheme or a power setting GUID.
              <ACTION>     Can be one of the following strings:
                           ActionSetActive, ActionCreate, ActionDefault 

 

Well this brings us to the conclusion of another week on the TechNet Script Center. Thank you for all of the questions sent to scripter@microsoft.com. Join us next week as we answer additional scripting questions.

If you want to know exactly what we will be looking at next week, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. Have a great weekend. Peace!

 

Ed Wilson and Craig Liebendorfer, Scripting Guys 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hey scripting guy, how can I use vbscript to check the current power settings and store the information in a text file?

    Thanks

    SR

  • thanks