Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Add for-each loop to this script that calls other functions

$
0
0

Hey All

I've been trying to get this script to work against multiple systems defined in a text file or csv file, but I'm stuck.

This script calls 2 other functions and I couldn't get them to work by using the "dot source", so I added the functions to my powershell session by doing ".\set-localusergroup.ps1" and ".\set-wmipermissions.ps1"

I found the script on this site:  http://vniklas.djungeln.se/2012/08/22/set-up-non-admin-account-to-access-wmi-and-performance-data-remotely-with-powershell/#comment-8600

Why won't they work in the way they are added in the script below?  I get an error that they aren't recognized as the name of a cmdlet or function.

Then how can I use this script in a loop, but keep the username standard and the domain standard across the systems I run this against? I found this on site and it seems to work well, but there were a few errors I had to fix in the scripts/functions.

There was an example of doing this in a loop by running the following, however that failed and I also have many systems to run this on, so importing a file would be best.

"computerone", "computertwo" | %{.\setrights.ps1 -Add -Username username -computername $_}

# Create Permissions for non-admin user on remote computers
#
# Niklas Akerlund / 2012-08-22
Param ([switch]$add,
	[switch]$remove,    $ComputerName = "1computer",    $UserName = "username",    $DomainName = "test")

# add functions to call
. .\Set-UserLocalGroup.ps1
. .\Set-WmiNamespaceSecurity.ps1

$LocalGroups = "Distributed COM Users","Performance Monitor Users"

if ($add){
	$LocalGroups | %{Set-UserLocalGroup -Computer $ComputerName -Group $_ -Domain $DomainName -User $UserName -add}
	Set-WMINamespaceSecurity root/CIMv2 add "$DomainName\$UserName" Enable,MethodExecute,ReadSecurity,RemoteAccess -computer $ComputerName
} elseif($remove) {
	$LocalGroups | %{Set-UserLocalGroup -Computer $ComputerName -Group $_ -Domain $DomainName -User $UserName -remove}
	Set-WMINamespaceSecurity root/cimv2 delete "$DomainName\$UserName" -computer $ComputerName

Viewing all articles
Browse latest Browse all 10624

Trending Articles