1

I have a script that opens a powershell console as admin and do sth in eventlog. I have two variables that i the new admin-PS console needs.

[string] $PiEventLog = "'Company Name Prv.Limt'"
[String] $PiEventLogSource = "'XY-Test'"

I am opening the new PS-Console like this

start powershell -Verb runas {
        If(Get-EventLog -List | ?{$_.Log -like $PiEventLog}){
            Write-Host "EventLog already exists." -ForegroundColor Yellow
        }
        else{
            New-EventLog -LogName $PiEventLog -Source $PiEventLogSource -ErrorAction Stop
            Write-Host "EventLog was successfully created." -ForegroundColor Green
        }
        Read-Host "Press any key to close the console..."
}

If i try to execute the script, i get the following error:

The argument for the parameter "LogName" cannot be checked. The argument is NULL or empty. Specify an argument that is not NULL or empty and re-execute the command.

anyone got an idea, how i can give those two variables to the new PS-console, without having to set two different variables in the new console?

Keeran
  • 302
  • 1
  • 15
  • Why do you `"'double-single'"` quote the strings? Use wildcards (*?) With the `-like` operator. Inside the scriptblock your variables are unknown. Try scoping these with `using:` – Theo Feb 01 '22 at 15:26
  • i can't use -like, because i need those variables in the new-eventlog command and without " ' xxx ' " the command doesn't recognize it as a variable. i don't know why. – Keeran Feb 01 '22 at 15:40
  • _i can't use -like_, but you ARE using `-like` here: `?{$_.Log -like $PiEventLog}` – Theo Feb 01 '22 at 15:51
  • @Theo I think he's trying to say _it doesn't work_ (presumably because `$PiEventLog` has no value) – Mathias R. Jessen Feb 01 '22 at 16:04
  • In short: [`Start-Process`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/start-process)'s `-ArgumentList` parameter (the 2nd positional parameter) only accepts _string(s)_, not script blocks (`{ ... }`). If you use the latter, the script block's _verbatim_ content is passed and variable references are therefore not recognized. The best approach is to use a single, expandable string (`"..."`) in which variable values can be embedded; see the [linked duplicate](https://stackoverflow.com/a/70815647/45375). – mklement0 Feb 01 '22 at 16:37
  • @MathiasR.Jessen yeah, that is what i wanted to say. Thank you :) – Keeran Feb 02 '22 at 07:12

1 Answers1

2

I believe this should work, it's easier if you use a Here-String. Since you're using the -like operator, I would assume you're looking for a Log that "contains" the input given in $PiEventLog, in that case, you should use wildcard characters: -like "*$PiEventLog*".

param(
    [string] $PiEventLog = 'Company Name Prv.Limt',
    [String] $PiEventLogSource = 'XY-Test'
)

$command = @"
If(Get-EventLog -List | Where-Object Log -Like '*$PiEventLog*'){
    Write-Host 'EventLog already exists.' -ForegroundColor Yellow
}
else{
    New-EventLog -LogName $PiEventLog -Source $PiEventLogSource -ErrorAction Stop
    Write-Host 'EventLog was successfully created.' -ForegroundColor Green
}
Read-Host "Press any key to close the console..."
"@

Start-Process powershell -Verb RunAs -ArgumentList '-c', $command

Then you call this script like:

PS /> ./script.ps1 -PiEventLog 'something' -PiEventLogSource 'something'
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    Nice, though it's worth calling out the fundamental problem with the approach in the question: attempting to use a _script block_ with `Start-Process`. This has come up several times recently, and I'm hoping that the linked duplicate clarifies that. – mklement0 Feb 01 '22 at 16:40
  • 1
    @mklement0 already had that one upvoted ;) and yes, it does clarify the problem OPs has but I still feel this will always be a recurrent issue / question being asked, until MS decides to document this better... – Santiago Squarzon Feb 01 '22 at 16:56
  • 1
    Yeah, adding a caveat to the `Start-Process` help topic might help, along the lines of "you may be tempted to pass a script block, but don't do that". If you feel so inspired (at least at the moment I don't), I encourage you to suggest that by creating an issue at https://github.com/MicrosoftDocs/PowerShell-Docs/issues/new/choose – mklement0 Feb 01 '22 at 17:00
  • @mklement0 Have been here enough to tell you're not feeling inspired lately :( on the other hand, I don't consider myself fit to open an issue (nor I feel the need to waste energy discussing something that is likely to not see the light) – Santiago Squarzon Feb 01 '22 at 17:06
  • 1
    I do hear you regarding potentially wasted energy. That said, you don't need to create a _PR_, necessarily - a _suggestion_ as to how to improve the docs (that can be implemented by someone else) should be enough. In fact, virtually all my contributions to the docs have come in the form of suggestions / bug reports, not in the form of PRs that fix the problem directly - but you never know what will actually be implemented. – mklement0 Feb 01 '22 at 17:13
  • 1
    @SantiagoSquarzon thank you. It works for me. I found that MS devblog page. But the explanation there didn't help me to understand it and even the page layout is a mess. Thank you again for your time. – Keeran Feb 02 '22 at 07:37
  • 1
    @mklement0 you are right. Your answer in that link would have helped aswell. I wish i saw that yesterday. No idea, how i overlooked that. It would have made my day easier, if i had found it yesterday. And also great explanation. Thank you aswell. i apprecitae your help. – Keeran Feb 02 '22 at 07:37