Powershell Error when run on a different machine - powershell

I wrote a script to take an AD user, disable the user, remove the user from group memberships and move the user to an OU. I originally wrote this on our Windows 2008 R2 DC (I know, bad idea) and I wanted to run the script locally on my Win 7 SP1 machine. It has the AD role installed as stated in this article (http://blogs.msdn.com/b/rkramesh/archive/2012/01/17/how-to-add-active-directory-module-in-powershell-in-windows-7.aspx)
I ran on both the DC and my Win7 machine $PSVersionTable and they are exactly the same. I can run ADSIEDIT.msc on the Win 7 machine. The error is occurring when doing an AD user lookup. See error output below:
Here is my script: https://github.com/nocode99/powershell/blob/master/UserDisableGroupRemoval.ps1
Property 'filter' cannot be found on this object; make sure it exists and is settable.
At C:\Admin\test.ps1:23 char:12
+ $ADsearch. <<<< filter = "(&(objectClass=user)(sAMAccountName=$user))"
+ CategoryInfo : InvalidOperation: (filter:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
You cannot call a method on a null-valued expression.
At C:\Admin\test.ps1:24 char:32
+ $ADfind = $ADsearch.findOne <<<< ()
+ CategoryInfo : InvalidOperation: (findOne:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas? The ActiveDirectory module imports with no issues and I want my users to run this locally on their machine rather than the DC.

Looks like I needed to include a filter before the lookup and added:
$adsearch = [adsisearcher]""
though I'm not sure why this works without the filter on AD server itself.

Related

InvalidCastException when trying to obtain UserPrincipal.Current

I have a PowerShell script which checks the currently signed in user as part of its start-up process. I'm using .Net to do this by adding the assembly:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$cUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
For almost everyone this works fine and I get a UserPrincipal object that I can use elsewhere, however there are a couple of users who get the following error when running it:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. At line:2 char:1
+ [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException
This is on a standard Windows 10 20H2 install and doesn't matter if PowerShell is running elevated or not. I've never seen this call return anything other than a UserPrincipal before, so I would be grateful for any advice: is this something I can deal with in code, or is there some underlying issue with the machines that are returning this exception?
I don't know if this is what you're looking for:
$cUser = Get-ADUser $Env:Username -Properties *
This has the most important attributes that you can use for whatever you want later on. It provides as much attribute as the code you posted that didn't work for some of the user.

Powershell InvokeGlobal Parameter count mismatch

I installed PSFramework on my W2k12 server. However when I run an import-module PSFramework I get the following error:
Exception calling "InvokeGlobal" with "0" argument(s): "Parameter count mismatch."
At C:\Program Files\WindowsPowerShell\Modules\psframework\1.6.181\PSFramework.psm1:7348 char:2
+ $isInstalled = $provider.IsInstalledScript.InvokeGlobal()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TargetParameterCountException
This works fine on my Win 10 machine.
Any ideas?
Thanks in advance,
Jake.
I'm the maintainer of PSFramework, sorry about that fail!
One of my security-related feature updates broke PSv3 & PSv4 support, I didn't notice right away and then was drowned in heavy workload to fix it timely.
I've added an issue that you can track until it is resolved:
https://github.com/PowershellFrameworkCollective/psframework/issues/474
In the meantime, you can resolve this on your server by updating its PowerShell to v5.1 as Mathias suggested, which will resolve it right away.

Getting error while exporting azure db using powershell

Followed Export SQL database example using link :
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-export-powershell#export-sql-database-example
Getting below error:
New-AzureRmSqlDatabaseExport : ResourceNotFound: The Resource 'Microsoft.Sql/servers/XXX.database.windows.net/databases/[DBNAME]' under resource group 'Default-SQL-SoutheastAsia' was
not found.
At [FilePath]\sample.ps1:24 char:18
+ $exportRequest = New-AzureRmSqlDatabaseExport –ResourceGroupName $ResourceGroupN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmSqlDatabaseExport], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.NewAzureSqlDatabaseExport
Get-AzureRmSqlDatabaseImportExportStatus : Cannot bind argument to parameter 'OperationStatusLink' because it is null.
At [FilePath]\sample.ps1:30 char:63
+ Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.Ope ...
+ CategoryInfo : InvalidData: (:) [Get-AzureRmSqlDatabaseImportExportStatus], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.GetAzureSqlDatabaseImportExportStatus
Any help?
I had this same issue today, actually. I think if you change your server name to not use the fully-qualified name, that might do it. So, just use "xxxxx."
At least that's what worked for me.
You need to verify that your Automation account has latest modules imported and are up to date (AzureRM.Automation – AzureRM.Profile – AzureRM.Sql) atleast to Version: 2.5.0. If modules show a different version, i.e. 1.0.3 then:
navigate to assets in the automation account, select modules and click on
Update Azure Modules
.
Wait for the modules to get updated it normally takes a few minutes

"Method Not found" error when calling GetElementsbyClassName

I created a PowerShell automation script and I gave it to my friend when he run it, it said
Method invocation failed because [mshtml.HTMLBodyClass] does not contain a method
named 'getElementsByClassName'.
At C:\Users\עמית\Documents\asaf.ps1:22 char:3
+ $a=$docs.body.getElementsByClassName("FadeOut-Scroll")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
why is that happened?
We both have IE11, .net 4.5, Visual studio, but I have that function and he not.
And it looks like here in his computer the IE console have the function:
but PowerShell does not:
How to update PowerShell?
Found after alot of time:
the missing want microsoft core xml

start-job loop how to call second script

Can anyone please suggest me what is wrong - I am calling second script from first so that
I can run the compare in background or parallel, due to bug in IDM software I need to
execute loop two times.
I need to call 5 scripts from my main script ( frist script) so that all five scripts run
parallelly.
First Script -
==================================================
Error message
Attribute cannot be added because it would cause the variable sbtFile with value C to become invalid.
+ CategoryInfo : MetadataError: (:) [Start-Job], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure,Microsoft.PowerShell.Commands.StartJobCommand
The command cannot find the job because the CompareCtrlMasterCtrlModelESS name was not found. Verify the value of the Name parameter, and then try the comman
d again.
+ CategoryInfo : ObjectNotFound: (CompareCtrlMasterCtrlModelESS:String) [Wait-Job], PSArgumentException
+ FullyQualifiedErrorId : JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.WaitJobCommand
The command cannot find the job because the CompareCtrlMasterCtrlModelESS name was not found. Verify the value of the Name parameter, and then try the comman
d again.
+ CategoryInfo : ObjectNotFound: (CompareCtrlMasterCtrlModelESS:String) [Receive-Job], PSArgumentException
+ FullyQualifiedErrorId : JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.ReceiveJobCommand
Regards
Naveen
You run Start-Job -Name "CompareCtrlMasterCtrlModelESS" in a loop, so you try to create multiple jobs with the same name. Try Start-Job -Name "CompareCtrlMasterCtrlModelESS$i" (with ordinal suffix).