Powershell Invoke command in a function - powershell

Im trying to do an invoke command inside a function, but having difficulty getting defined variables to pass through.
function Set-RemoteLocalAdmin {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$ComputerName,
[String] $UserId
)
$sb = {​
param($userid)
add-LocalGroupMember -Group "Administrators" -Member "$UserId"}
Invoke-Command -ComputerName $ComputerName -Scriptblock $sb -argumentlist $userid
}
Errors:
The term '​' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (​:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : COMPUTERNAME-1
The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : COMPUTERNAME-1
Member BUILTIN\ was not found in group Administrators.
+ CategoryInfo : ObjectNotFound: (BUILTIN:String) [Add-LocalGroupMember], MemberNotFoundException
+ FullyQualifiedErrorId : MemberNotFound,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand
+ PSComputerName : COMPUTERNAME-1
What am I missing here?
Thanks in advance.

Related

PowerShell Exchange online Get-DistributionGroup cmdlet returns errors on some of its attributes

enter code hereFind my script and the error messages I'm getting on some of get-distribution group attributes.
The attributes are:
managedby
memberjoinrestriction
memberdepartrestriction
requiresenderauthenticationenabled
When i exclude the the above attributes, the scripts returns well without error.
Assist me to understand where i'm doing it wrong.
The script: The script is supposed to bulk export information for distribution groups
$Groups = Get-DistributionGroup –ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_.Name
$primarySMTPAddress = $_.PrimarySMTPAddress
$managedby = $._ManagedBy
$memberjoinrestriction = $._MemberJoinRestriction
$memberdepartrestriction = $._MemberDepartRestriction
$requiresenderauthenticationenabled = $._RequireSenderAuthenticationEnabled
Get-DistributionGroupMember $group | ForEach-Object {
New-Object -TypeName PSObject -Property #{
Group = $group
GroupPrimarySMTPAddress = $primarySMTPAddress
Member = $_.Name
ManagedBy = $managedby
MemberJoinRestriction = $memberjoinrestriction
MemberDepartRestriction = $memberdepartrestriction
RequireSenderAuthenticationEnabled = $requiresenderauthenticationenabled
EmailAddress = $_.PrimarySMTPAddress
}}} | Export-CSV "C:\DistributionGroupinfov1.csv" -NoTypeInformation -Encoding UTF8```
The error message is
```$._ManagedBy : The term '$._ManagedBy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:5 char:14
+ $managedby = $._ManagedBy
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ($._ManagedBy:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
$._MemberJoinRestriction : The term '$._MemberJoinRestriction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At line:6 char:26
+ $memberjoinrestriction = $._MemberJoinRestriction
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ($._MemberJoinRestriction:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
$._MemberDepartRestriction : The term '$._MemberDepartRestriction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:7 char:28
+ $memberdepartrestriction = $._MemberDepartRestriction
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ($._MemberDepartRestriction:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
$._RequireSenderAuthenticationEnabled : The term '$._RequireSenderAuthenticationEnabled' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:8 char:39
+ ... resenderauthenticationenabled = $._RequireSenderAuthenticationEnabled
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ($._RequireSenderAuthenticationEnabled:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException```
When i run the command individually with a single attribute, it returns well without error
```Get-DistributionGroup -identity "support" | Format-table RequireSenderAuthenticationEnabled```

'help' command is not working in my windows powershell

So I typed the command 'help' in windows Powershell but I'm getting the following error:
more.com : The term 'more.com' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:9 char:19
+ } else { $input | more.com }
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (more.com:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-ObjectAcl' is not recognized as the name of a cmdlet

When i tried run this PS Script :
" Get-ObjectAcl -SamAccountName delegate -ResolveGUIDs | ? {$_.ActiveDirectoryRights -eq "GenericAll"} "
i got error like that:
Get-ObjectAcl : The term 'Get-ObjectAcl' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
Get-ObjectAcl -SamAccountName delegate -ResolveGUIDs | ? {$_.ActiveDi ...
+ CategoryInfo : ObjectNotFound: (Get-ObjectAcl:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I searched but i can't find how can i use module Add-ObjectAcl and Get-ObjectAcl

Issue with PS1 File running as System

I am trying to execute a PS1 file that uses the new PS 5.1 cmdlet Remove-LocalUser and am running into some issues. Below is the code block that I am running which will check for any local accounts in the Administrator group and then delete them if they are not in the $IgnoreList:
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\BigFix\AdminAccounts\output.txt -append
$users = Get-WmiObject win32_groupuser | Where-Object { $_.GroupComponent -match ‘administrators’ } | ForEach-Object {[wmi]$_.PartComponent }
$IgnoreList = "SOMEACCOUNTNAME"
#Write-host $users.name
:OuterLoop
foreach ($user in $users) {
foreach ($account in $IgnoreList) {
if ($user.name -like "$account") {
continue OuterLoop
}
}
Remove-LocalUser -Name $user.Name
}
Stop-Transcript
This script runs fine on the target machine if I am logged in to a local admin account but will not run when deployed through our End Point management console where it runs as the system account.
The machine in question is running the following version of PS:
Name Value
---- -----
PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.36373
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Here is the output of the transcript file when run by the System account:
**********************
Windows PowerShell transcript start
Start time: 20170213172829
Username: **DOMAIN**\SYSTEM
RunAs User: **DOMAIN**\SYSTEM
Machine: QA-G2-CUCNOV-V4 (Microsoft Windows NT 6.1.7601 Service Pack 1)
Host Application: powershell.exe -executionpolicy unrestricted -NonInteractive -NoProfile -file c:\BigFix\AdminAccounts\removeAdmins.ps1
Process ID: 3244
PSVersion: 5.1.14409.1005
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1005
BuildVersion: 10.0.14409.1005
CLRVersion: 4.0.30319.36373
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\BigFix\AdminAccounts\output.txt
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+ Remove-LocalUser -Name $user.Name
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Remove-LocalUser:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
**********************
Windows PowerShell transcript end
End time: 20170213172829
**********************
Any thoughts on why this may be happening would be greatly appreciated!

Issue while executing simple PS script

PS C:> c:\test.ps1
Error:
*The term 'c:\test.ps1' is not recognized as the name of a
spelling of the name, or if a path was included, verify t
At line:1 char:12
+ c:\test.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (c:\test.ps1
+ FullyQualifiedErrorId : CommandNotFoundException*
My Script contains
$letterArray = "a","b","c","d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
Move the script to another(deeper) folder and try again? You can also try
& c:\test.ps1