The start-process cmdlet of powershell will fail with -PassThru argument when trying to launch Edge browser - powershell

In my test script by powershell, I'd start an Edge browser and get it's main process's ID. Simply like this:
$edge = Start-Process microsoft-edge: www.ted.com -PassThru
but instead of able to get the process id from $edge.Id, I've got error message like this:
Start-Process : This command cannot be run completely because the system cannot find all the information required.
At line:1 char:7
+ $edge=Start-Process microsoft-edge: www.ted.com -PassThru
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Is there any simple and straightforward way to really doing this without enumerate and check the process list?
Thanks!

I try to test your script and I am getting a similar error as yours.
Based on my search result, passthru is not one of the common parameters, and it does not exist everywhere.
Reference:
Use the PowerShell Passthru Parameter and Get Back Objects
I think it is not available for the MS Edge browser and that's why the script gives an error.
I try to search for an alternative command for PassThru but I did not get any helpful information about it.
In this situation, it is better to enumerate and check the process list

Related

PowerShell Import-GPO: Operation not valid

Afternoon everyone. I'm running into an issue I'm not sure how to handle. I'm working on a script for work to deploy a Domain Controller using PSremoting. It all works well in fine until I get to where I'm importing some GPOs from backups.
*All the commands are run under invoke-command
I run the command Import-GPO -BackUpName $GPO -TargetName $GPO -Path $GPOPath -MigrationTable $MigTable -CreateIfNeeded
When I run this, I get an error on the host:
Operation is not valid due to the current state of the object.
+ CategoryInfo : NotSpecified: (:) [Import-GPO], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.GroupPolicy.Commands.ImportGpoCommand
+ PSComputerName : v204-DC1
I can't seem to find anything that says what this means. When I check for the GPOs on the DC, they all show up and seem to be linked properly. I am curious what this error is, or if I should just append -ErrorAction SilentlyContinue to the end of my code.
Ended up being an issue with my migration table. I rolled back to an earlier one and ended up being good to go

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 DSC: The data source could not process the filter

Afternoon!
I have run into an issue with PowerShell DSC (the Start-DscConfiguration cmdlet specifically) 2 days trying to figure it out, now I am here :)
I am getting the following error for a specific MOF file, prior MOFs run fine.
The data source could not process the filter. The filter might be missing or it might be invalid. Change the filter
and try the request again.
+ CategoryInfo : InvalidArgument: (root/Microsoft/...gurationManager:String) [], CimException
+ FullyQualifiedErrorId : HRESULT 0x8033801a
EDIT: Has anyone experienced this error before? I can share the MOF file in question, with some restricted info removed.
Thank you
The issue here wasn't with PowerShell DSC, rather just PowerShell in itself.
I had a cmdlet within a switch block. As an example:
Switch($item){
{$_ -eq $true}{do something}
{$_ -eq $fales}{do something}
get-service -name $item
}
It was the get-service cmdlet that could not be filtered.

text manipulation with powershell

I'm trying to format the output of this command to get only the dfsnamespace only like that :
\\F-TYPHON\DATA13\AI-Project
I can not use the Get-DfsnFolderTarget cmdlet because the RSAT-DFS-Mgmt-Con
is not installed on all servers and I cannot install it .
$DFSPath="\\F-TYPHON\shared\AI-Project"
PS C:\> dfsutil client property state $DFSPath
Active, Online \\F-TYPHON\DATA13\AI-Project
Done processing this command.
I've tried this .
PS C:\> $dfs=dfsutil client property state $DFSPath
PS C:\> $dfs.trimstart("Active, Online")
Method invocation failed because [System.Object[]] doesn't contain a method named 'trimstart'.
At line:1 char:15
+ $dfs.trimstart <<<< ("Active, Online")
+ CategoryInfo : InvalidOperation: (trimstart:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
any help will be apreciated I can list all volume data for the filer but there's many incoherence in the structure so I need only to list the shared folder under "shared" on a filer and then procces it with dfsutil to get the absolut path
Use a regular expression to match the text output of dfsutil:
$DFSPath="\\F-TYPHON\shared\AI-Project"
if ((dfsutil client property state $DFSPath) -match "(?<=\s{2,})\\\\.*"){
$DFSNameSpace = $Matches.Value
}
here (?<=\s{2,})\\\\.* matches two or more whitespace \s in a lookbehind
followed by two (escaped) backslashes and the remainder of the line.
thank you for your help
i've found a solution that i can use in a way it's not 100% powershell but i can use it to extract the output in a format that i can use it in a loop and than make automated robocopylines with powershell
here the code for the output that i found
$DFSPath="\F-TYPHON\shared\AI-Project"
$dfspath=(dfsutil client property state $DFSPath |findstr /i \F-TYPHON)|out-string
$dfs=$dfspath.Trimstart("Active, Online ")
write-host $dfs
the output is
\F-TYPHON\DATA13\AI-Project
it's certainly not the best but i can work with it
if you have any others suggestion you're welcome

How to use powershell to open a URL like "imacros://run/?m=my_saved_macro.iim"?

To open a typical URL like http://www.google.com from PowerShell, we can use something like:
start http://www.google.com
However, to automate imacros, sometimes I need open URLs like imacros://run/?m=my_saved_macro.iim. You can go to here if you want the details.
When I try the PowerShell code start imacros://run/?m=my_saved_macro.iim, I got the following error:
start : This command cannot be run due to the error: Unknown error (0x80041002).
At c:\temp\test.ps1:51 char:5
+ start "imacros://run/?m=$macro_file_name";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
I want to know how to open this kind of URL in the default browser from PowerShell.
Thanks.
You should be able to use the Start-Process cmdlet like so:
Start-Process $URI
If you need even more control, you can go into the .NET API for Process and use one of the modifications of this:
[System.Diagnostics.Process]::Start($URI)