Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
adaz
Mar 7, 2009

Submarine Sandpaper posted:

ah that explains the error on my personal and the read-only exception.

But it's still an error on the client's machines:
Exception calling "SetAllowTSConnections" : "Invalid Operation"

Hopefully it just won't be needed for this client's task :shrug:
I think these folks have an AV that's been sold to me as blocking all this and I don't have the keys to prevent that.

If it's AV blocking it _most_ will log something either in the event log or in a disk log. Ran into this recently too

Adbot
ADBOT LOVES YOU

myron cope
Apr 21, 2009

I see Powershell Core (they don't even call it that anymore, do they?) is up to 7.1 now. Is it able to replace 5.1 yet* for Windows administration stuff? The last time I tried it (which was maybe even before the 6.0 release) it was very limited in what cmdlets it could use--like the AD cmdlets for example, I think a lot of those didn't work. While I do work with some Linux so it would be cool to get Powershell there, I mainly use it with Windows.

*I don't even know if that's even the goal? I know when 6 came out they mentioned keeping 5.1 for this kind of stuff

Pile Of Garbage
May 28, 2007



myron cope posted:

I see Powershell Core (they don't even call it that anymore, do they?) is up to 7.1 now. Is it able to replace 5.1 yet* for Windows administration stuff? The last time I tried it (which was maybe even before the 6.0 release) it was very limited in what cmdlets it could use--like the AD cmdlets for example, I think a lot of those didn't work. While I do work with some Linux so it would be cool to get Powershell there, I mainly use it with Windows.

*I don't even know if that's even the goal? I know when 6 came out they mentioned keeping 5.1 for this kind of stuff

According to this roadmap post: https://devblogs.microsoft.com/powershell/powershell-7-road-map/

quote:

The PowerShell Team will be working with Windows teams to validate and update their modules to work with PowerShell 7. This also means that to use PowerShell 7 with the breadth of Windows PowerShell modules, you will need to be using the latest builds of Windows 10 (and equivalent Windows Server).

So compatibility with modules will be dependent on the system you import them on. You can probably install PowerShell 7 on a Server 2016 machine but I wouldn't expect everything to work with it, however if you install it on your Win10 PC running the latest build with RSAT then you'll have better luck.

IMO if you're system is setup for it then I see no issues with using it for interactive admin stuff. However when it comes to writing scripts you should always be targeting the minimum PSVersion in your environment.

Gerdalti
May 24, 2003

SPOON!
Quick question for the pros.

I'm writing a script to install Chrome (MSI or EXE) through remote-pssession. However, I can't turn off UAC on my systems, and Chrome's installer wants to elevate (or pop up a warning that it will be installed only for the user that starts the remote-pssession.

Obviously, I'm in a remote powershell session, so I can't hit Yes on the UAC prompt, so the installer never runs. I've tried running the MSI/EXE directly, or by executing msiexec with /qn /i etc. Nothing seems to work.

Unfortunately, this is in a no-internet zone, and it's in a NASA facility, but governed by DOI and USGS. I can't just grab PDQ and deploy this poo poo properly (and our SCCM has been decommissioned), however we still have to meet 7 day "patch" mandates by the government, so we have to be able to patch even though the systems only use Chrome for internal applications.

Save me!

Dirt Road Junglist
Oct 8, 2010

We will be cruel
And through our cruelty
They will know who we are

Gerdalti posted:

Quick question for the pros.

I'm writing a script to install Chrome (MSI or EXE) through remote-pssession. However, I can't turn off UAC on my systems, and Chrome's installer wants to elevate (or pop up a warning that it will be installed only for the user that starts the remote-pssession.

Obviously, I'm in a remote powershell session, so I can't hit Yes on the UAC prompt, so the installer never runs. I've tried running the MSI/EXE directly, or by executing msiexec with /qn /i etc. Nothing seems to work.

Unfortunately, this is in a no-internet zone, and it's in a NASA facility, but governed by DOI and USGS. I can't just grab PDQ and deploy this poo poo properly (and our SCCM has been decommissioned), however we still have to meet 7 day "patch" mandates by the government, so we have to be able to patch even though the systems only use Chrome for internal applications.

Save me!

Can you use Chocolatey?

Barring that, StackExchange says chrome_installer.exe /silent /install is the set of switches you need, but I haven't confirmed that personally.

Gerdalti
May 24, 2003

SPOON!

Dirt Road Junglist posted:

Can you use Chocolatey?

Barring that, StackExchange says chrome_installer.exe /silent /install is the set of switches you need, but I haven't confirmed that personally.

Unfortunately no to chocolatey.
And I could not get the /silent /install to actually do anything, but I may need to test a bit more since my script had evolved.
A buddy suggested making a scheduled task in the script that runs as system, which I could then delete after forcing it to run. Might work too.

Nth Doctor
Sep 7, 2010

Darkrai used Dream Eater!
It's super effective!


Gerdalti posted:

Unfortunately no to chocolatey.
And I could not get the /silent /install to actually do anything, but I may need to test a bit more since my script had evolved.
A buddy suggested making a scheduled task in the script that runs as system, which I could then delete after forcing it to run. Might work too.

What happens if your remote pssession is administrator? Presuming that's even possible.

Gerdalti
May 24, 2003

SPOON!

Nth Doctor posted:

What happens if your remote pssession is administrator? Presuming that's even possible.

Hmm, good question. I'll test that too. Got a few ideas now, I'll test this weekend and report back.

Toast Museum
Dec 3, 2005

30% Iron Chef

Gerdalti posted:

Quick question for the pros.

I'm writing a script to install Chrome (MSI or EXE) through remote-pssession. However, I can't turn off UAC on my systems, and Chrome's installer wants to elevate (or pop up a warning that it will be installed only for the user that starts the remote-pssession.

Obviously, I'm in a remote powershell session, so I can't hit Yes on the UAC prompt, so the installer never runs. I've tried running the MSI/EXE directly, or by executing msiexec with /qn /i etc. Nothing seems to work.

Unfortunately, this is in a no-internet zone, and it's in a NASA facility, but governed by DOI and USGS. I can't just grab PDQ and deploy this poo poo properly (and our SCCM has been decommissioned), however we still have to meet 7 day "patch" mandates by the government, so we have to be able to patch even though the systems only use Chrome for internal applications.

Save me!

For the enterprise MSI installer, at least, you definitely can install from a remote session with /quiet or /passive if you have administrator privileges on that machine.

Potato Salad
Oct 23, 2014

nobody cares


Man, gently caress whoever got rid of sccm in an environment where you need weekly patching assurance

epm is there to help you MEET compliance requirements, what doofus did that?

Pile Of Garbage
May 28, 2007



Potato Salad posted:

Man, gently caress whoever got rid of sccm in an environment where you need weekly patching assurance

epm is there to help you MEET compliance requirements, what doofus did that?

Some bean-counter probably saw the Microsoft true-up bill and thought "we don't need this yeah"

MJP
Jun 17, 2007

Are you looking at me Senpai?

Grimey Drawer
Got what is probably a dumb variable passing question.

I have a CSV. It has two columns: subscriptionName and annualCost.

I need to run
code:
New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName %name of subscription goes here% -annualCost %annual cost for the subscription goes here%
once for each subscription in subscriptionName.

Is that just a matter of something like this?
code:
import-csv subscriptionFile.csv
foreach ($subscriptionName in $subscriptionNames$){New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName $subscriptionName -annualCost $annualcost}

The Fool
Oct 16, 2003


MJP posted:

Got what is probably a dumb variable passing question.

I have a CSV. It has two columns: subscriptionName and annualCost.

I need to run
code:
New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName %name of subscription goes here% -annualCost %annual cost for the subscription goes here%
once for each subscription in subscriptionName.

Is that just a matter of something like this?
code:
import-csv subscriptionFile.csv
foreach ($subscriptionName in $subscriptionNames$){New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName $subscriptionName -annualCost $annualcost}

more like this:

code:
$subscriptionFile = import-csv subscriptionFile.csv
foreach ($row in $subscriptionFile) {
    New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName $row.subscriptionName -annualCost $row.annualcost
}
You need to import your csv into a variable or use the pipeline. I prefer the variable route as it's more readable and easier to manage.

ForEach will iterate over your file object, processing each row. You access the columns as properties of the row object.

The Fool fucked around with this message at 17:18 on Nov 19, 2020

MJP
Jun 17, 2007

Are you looking at me Senpai?

Grimey Drawer
Thanks tons! Just realized I now have to do this per subscription, and give them unique names, so now I need to run select-azsubscription -subscriptionName %subscription name goes here% and then run the new-AzSubscriptionDeployment script D:

Am I looking at this, then?
code:
$subscriptionFile = import-csv subscriptionFile.csv
foreach ($row in $subscriptionFile) {(Select-AzSubscription -subscriptionName $row.subscriptionName)

    New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName"_Annual" -annualCost $row.annualcost
}

The Fool
Oct 16, 2003


MJP posted:

Thanks tons! Just realized I now have to do this per subscription, and give them unique names, so now I need to run select-azsubscription -subscriptionName %subscription name goes here% and then run the new-AzSubscriptionDeployment script D:

Am I looking at this, then?
code:
$subscriptionFile = import-csv subscriptionFile.csv
foreach ($row in $subscriptionFile) {(Select-AzSubscription -subscriptionName $row.subscriptionName)

    New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName"_Annual" -annualCost $row.annualcost
}

Not testing this, but I don't think inlining the string works in that situation.

You could do this:
code:
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName + "_Annual" -annualCost $row.annualcost
or:
code:
$budgetName = $row.subscriptionName + "_Annual"
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $budgetName -annualCost $row.annualcost

LegoMan
Mar 17, 2002

ting ting ting

College Slice
code:
if( -not ($latest_source)) {
    Write-Host "No Valid Source File Found."
    break
}
This is causing an unhandled exception (I added it from what you posted because as stated before if there's no file there it will continue happily creating things anyways)

I tried break, exit

is there something I can put it to just start at the beginning?

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

LegoMan posted:

code:
if( -not ($latest_source)) {
    Write-Host "No Valid Source File Found."
    break
}
This is causing an unhandled exception (I added it from what you posted because as stated before if there's no file there it will continue happily creating things anyways)

I tried break, exit

is there something I can put it to just start at the beginning?

Post your error message. You have done something very wrong.

mystes
May 31, 2006

The Fool posted:

You could do this:
code:
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName + "_Annual" -annualCost $row.annualcost
Wouldn't you need parentheses?

MJP
Jun 17, 2007

Are you looking at me Senpai?

Grimey Drawer

The Fool posted:

Not testing this, but I don't think inlining the string works in that situation.

You could do this:
code:
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName + "_Annual" -annualCost $row.annualcost
or:
code:
$budgetName = $row.subscriptionName + "_Annual"
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $budgetName -annualCost $row.annualcost

No luck - + isn't accepted as a positional parameter for New-AzSubscriptionDeployment. It looks like you may indeed be right about inlining.

I was able to at least concatenate the names and put them into an additional column in the CSV - I'll have to futz to get it into our test environment but that might work around the issue.

Thanks for taking the time to help!

The Fool
Oct 16, 2003


MJP posted:

No luck - + isn't accepted as a positional parameter for New-AzSubscriptionDeployment. It looks like you may indeed be right about inlining.

I was able to at least concatenate the names and put them into an additional column in the CSV - I'll have to futz to get it into our test environment but that might work around the issue.

Thanks for taking the time to help!

Actually, mystes is right and I forgot parentheses in that example

code:
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName ($row.subscriptionName + "_Annual") -annualCost $row.annualcost

LegoMan
Mar 17, 2002

ting ting ting

College Slice

Toshimo posted:

Post your error message. You have done something very wrong.


even better I'll post the function

code:
Function viscopy (){
    $source_prefix = "VIS"                                         #Filter condition that only looks at arm files (in case there is a more recent visual data file)
    $output_prefix = $vis_path + "\M3VIS"                       #change to name of AGV arm file -AGV number
    $output_suffix = ".dat"
    $latest_source = Get-ChildItem -Path $disk_path -Filter "*$($source_prefix)*.dat" | where-object { -not $_.PSIsContainer } | Sort -Descending LastWriteTime | Select -First 1        #grabs latest file off disk based on filter above. Removes need for specifying which AGV used for teach
    if( -not ($latest_source)) {
        Write-Host "No Valid Source File Found."
        break}
    Write-Host "Duplicating $($latest_source.Name) $agv_num times..."
        foreach( $iteration in 1..$agv_num) {
    $pad_length = ([string]$agv_num).Length
    $source_path = $disk_path + "\" + $latest_source                                                                                                                                     #begin section for copying AGV file for every AGV
    $output_path = $output_prefix + ([string]$iteration).PadLeft($pad_length,"0") + $output_suffix
    Copy-Item $source_path -Destination $output_path -Force
    Write-Host "$($output_path) created."}}
It worked beautifully until I put in the check to see if $latest_source actually had a value. ( if( -not ($latest_source)) )

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

LegoMan posted:

even better I'll post the function

code:
Function viscopy (){
    $source_prefix = "VIS"                                         #Filter condition that only looks at arm files (in case there is a more recent visual data file)
    $output_prefix = $vis_path + "\M3VIS"                       #change to name of AGV arm file -AGV number
    $output_suffix = ".dat"
    $latest_source = Get-ChildItem -Path $disk_path -Filter "*$($source_prefix)*.dat" | where-object { -not $_.PSIsContainer } | Sort -Descending LastWriteTime | Select -First 1        #grabs latest file off disk based on filter above. Removes need for specifying which AGV used for teach
    if( -not ($latest_source)) {
        Write-Host "No Valid Source File Found."
        break}
    Write-Host "Duplicating $($latest_source.Name) $agv_num times..."
        foreach( $iteration in 1..$agv_num) {
    $pad_length = ([string]$agv_num).Length
    $source_path = $disk_path + "\" + $latest_source                                                                                                                                     #begin section for copying AGV file for every AGV
    $output_path = $output_prefix + ([string]$iteration).PadLeft($pad_length,"0") + $output_suffix
    Copy-Item $source_path -Destination $output_path -Force
    Write-Host "$($output_path) created."}}
It worked beautifully until I put in the check to see if $latest_source actually had a value. ( if( -not ($latest_source)) )

I tried this with just dropping some variables at the bottom and running the function and it worked as-is. I'd need to see your error message to know more.

code:
PS C:\tmp> Function viscopy (){
    $source_prefix = "VIS"                                         #Filter condition that only looks at arm files (in case there is a more recent visual data file)
    $output_prefix = $vis_path + "\M3VIS"                       #change to name of AGV arm file -AGV number
    $output_suffix = ".dat"
    $latest_source = Get-ChildItem -Path $disk_path -Filter "*$($source_prefix)*.dat" | where-object { -not $_.PSIsContainer } | Sort -Descending LastWriteTime | Select -First 1        #grabs latest file off disk based on filter above. Removes need for specifying which AGV used for teach
    if( -not ($latest_source)) {
        Write-Host "No Valid Source File Found."
        break}
    Write-Host "Duplicating $($latest_source.Name) $agv_num times..."
        foreach( $iteration in 1..$agv_num) {
    $pad_length = ([string]$agv_num).Length
    $source_path = $disk_path + "" + $latest_source                                                                                                                                     #begin section for copying AGV file for every AGV
    $output_path = $output_prefix + ([string]$iteration).PadLeft($pad_length,"0") + $output_suffix
    Copy-Item $source_path -Destination $output_path -Force
    Write-Host "$($output_path) created."}}

$diskpath = "C:\tmp"
$vis_path = "C:\tmp"
$agv_num = 3

viscopy
Duplicating VIS.dat 3 times...
C:\tmp\M3VIS1.dat created.
C:\tmp\M3VIS2.dat created.
C:\tmp\M3VIS3.dat created.

PS C:\tmp> 

LegoMan
Mar 17, 2002

ting ting ting

College Slice

Toshimo posted:

I tried this with just dropping some variables at the bottom and running the function and it worked as-is. I'd need to see your error message to know more.

code:
System.Management.Automation.BreakException: System error.
   at System.Management.Automation.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   at System.Management.Automation.DlrScriptCommandProcessor.Complete()
   at System.Management.Automation.CommandProcessorBase.DoComplete()
   at System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(CommandProcessorBase commandRequestingUpstreamCommandsToStop)
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Just feels weird that everything works fine until that simple if statement comes in

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
Try replacing your "break" with a "return(0)" while I try and type something more meaningful up.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
So, my understanding of it, and I'm sure someone with better knowledge will come in and correct all the dumb things I am about to say, is this:

Once you start adding the GUI stuff on top, some of the typical PowerShell flow control bits like "break" and "exit" aren't going to work because the GUI wrapper expects a response from each action and bouncing out that way just returns NULL which it doesn't like.

So, you have to be careful with your flow control and use return, particularly with a return value, when possible.

LegoMan
Mar 17, 2002

ting ting ting

College Slice

Toshimo posted:

So, my understanding of it, and I'm sure someone with better knowledge will come in and correct all the dumb things I am about to say, is this:

Once you start adding the GUI stuff on top, some of the typical PowerShell flow control bits like "break" and "exit" aren't going to work because the GUI wrapper expects a response from each action and bouncing out that way just returns NULL which it doesn't like.

So, you have to be careful with your flow control and use return, particularly with a return value, when possible.

That makes sense in that I know what returns are for and the return(0) worked so thanks again. (Check your PMs)

Pile Of Garbage
May 28, 2007



Toshimo posted:

So, my understanding of it, and I'm sure someone with better knowledge will come in and correct all the dumb things I am about to say, is this:

Once you start adding the GUI stuff on top, some of the typical PowerShell flow control bits like "break" and "exit" aren't going to work because the GUI wrapper expects a response from each action and bouncing out that way just returns NULL which it doesn't like.

So, you have to be careful with your flow control and use return, particularly with a return value, when possible.

There's your problem.

LegoMan
Mar 17, 2002

ting ting ting

College Slice

Pile Of Garbage posted:

There's your problem.

I'm not designing it for myself. It's going to be something (hopefully) over 100 employees will use for their local sites. It has to be something as simple as possible because the range of computer skills is pretty wide.

It doesn't have to be fancy, it just needs to be easy to use. With the added "return(0)" everything clicks together (including if there's no positive result from the get command).

mystes
May 31, 2006

First of all, the break statement appears to be outside of a loop so are you trying to break out of your scripts execution and return control to the REPL? Otherwise what are you trying to accomplish with a break statement here? I don't really understand why you have a break statement here at all; I didn't think it was really encouraged to use it like this but I'm not that knowledgeable about powershell or what's considered good/idomatic powershell style.

Also, it's MUCH harder to write gui stuff in powershell and get it working correctly than to use c#. This is not related to your question, but you really need to not be doing everything in one thread if that's what you're doing, but it's very annoying to try to fix that in powershell.

mystes fucked around with this message at 21:15 on Nov 21, 2020

LegoMan
Mar 17, 2002

ting ting ting

College Slice
It actually works very well. It breaks if there's no positive result from the get command and displays a pop-up informing the user of that fact.

I had originally written the program in C# but the powers that be decided they considered anything not Powershell an "app" and slapped a shitload of restrictions and security checks on it. It's a simple program that copies files from one place to another and is simply automating a tedious task, it doesn't need to be a flawless masterpiece.

Also the break is in a loop (but it's not a break anymore, it's a return)

mystes
May 31, 2006

Have you considered just copying your c# code into powershell and using add-type?

LegoMan
Mar 17, 2002

ting ting ting

College Slice

mystes posted:

Have you considered just copying your c# code into powershell and using add-type?

no because I'm not really a trained programmer (which is why I had to ask goons for something as simple as modifying a string and putting the value into a variable) and I didn't even know add-type exists. I'll keep that in mind thank you.

Pile Of Garbage
May 28, 2007



LegoMan posted:

Also the break is in a loop (but it's not a break anymore, it's a return)

I've had issues in the past with break and loops, found the best way was to always used labeled break statements: https://docs.microsoft.com/en-us/po...break-in-a-loop

Lum
Aug 13, 2003

Anyone know what might be going on with tempting in this setup

So we have two domains at 2012 functional level with a domain trust between them. They're in physically separate locations.

If I log into, Server1.DomainA.local (Server 2016) as my domain admin account, open an admin Powershell (5.1) and do Enter-PSSession Server3.DomainB.local it lets me in fine without a password prompt. This also works fine for Server 4&5 in DomainB.

On Server2.DomainA.local (also Server 2016) I can do this to connect to server 4&5, but not Server3. I get a "user name or password incorrect" error.

If I add -Credential and give my DomainA\DomAdmin account and password, then it works.

I'm not even using a password because there's a domain trust and it's all Kerberos delegation etc.

All servers have had Enable-PSRemoting -Force run on them.

What do I need to check to get this working, all googling points to people having this issue on non-domain setups.

i am a moron
Nov 12, 2020

"I think if there’s one thing we can all agree on it’s that Penn State and Michigan both suck and are garbage and it’s hilarious Michigan fans are freaking out thinking this is their natty window when they can’t even beat a B12 team in the playoffs lmao"
Probably not a powershell thing. Sounds like DNS, specifically you have a server that is unable to reach the other domain cause it’s not pointing to the right place or the search suffix isn’t the same on all of them.

Lum
Aug 13, 2003

It can reach it because it fails instantly, and logging in with -Credential works

BeastOfExmoor
Aug 19, 2003

I will be gone, but not forever.
I've had a couple nagging Powershell questions I've been meaning to ask this thread, because I can't seem to find an easy way to do it.

First, I'm importing a CSV file using import-CSV, and assigning that to an object. Before moving on to processing, I'd like to ensure that the object contains a property such as "IP Address". I would have expected that something like the following would work:

code:
$computers = Import-CSV -Path ".\list.csv"

if(-not $computers."IP Address") {
    Write-Error 'CSV File does not contain the column for "IP Address".'
}
That doesn't work nor does anything else I've tried. Something like -not $computer[0]."IP Address" works, but has the downside of returning as true if the first line of a CSV happens to have a blank IP address.

Second, is there a simple way to import an XLS file's contents? I've found PSExcel, but it appears it only opens XLSX files and unfortunately the really old tool I'm using exports to XLS only for some horrible reason.

Lastly, is there a way to get a function that's expecting open a file and do something with the data to actually take data from a string? For example, I have CSV files that have a first line which defines the separator character (IE. "sep=,") before the actual CSV data, which breaks Import-CSV. I've resorted to importing the file as a string, removing the first line, resaving it to a temporary file, importing the temp file with Import-CSV, and deleting the temp file. I'd much rather just skip saving and deleting the temp file and just send the string straight to Import-CSV, but I can't find a way to do that.

Happiness Commando
Feb 1, 2002
$$ joy at gunpoint $$

I'm not exactly clear on what's failing for you - when I evaluate $computers[0].'Ip address', I find that a null value doesn't return true. Are you looking to evaluate if each CSV line has an IP address, or the CSV as a whole?
code:

PS C:\Users\Happiness Commando> $computers

Name IP Address
---- ----------
foo            
bar  1.1.1.1   

PS C:\Users\Happiness Commando> if ($computers[0].'IP Address') {"true"}

PS C:\Users\Happiness Commando> if ($computers[1].'IP Address') {"true"}
true
If you want to test the CSV as a whole, you can use Select-Object
code:
if (-not ($computers | Select-Object -Property "ip address")) { Do-Stuff } #I've gotten in trouble with -not precedence before so I use lots of parenthesis
If you want to test that each line item has an IP Address, I don't see why you couldn't enumerate across the whole thing and check for null or whitespace
code:
$computers | % {
    if ([String]::IsNullOrWhiteSpace($_.'ip address')) {
        "no IP"
    }
    else {
        Do-Stuff
    }
}
This zero upvote Stack answer offers a solution to working with XLS files, you would have to validate that it actually works.

I don't know how to do #3, and I would be surprised if there was a reasonable way. My solution would be to preprocess the data so that you have an actual CSV instead of a pseudo-CSV with a separator indicator prepended to a proper CSV - which is what you're doing.

Happiness Commando fucked around with this message at 02:43 on Dec 22, 2020

mystes
May 31, 2006

BeastOfExmoor posted:

I've had a couple nagging Powershell questions I've been meaning to ask this thread, because I can't seem to find an easy way to do it.

First, I'm importing a CSV file using import-CSV, and assigning that to an object. Before moving on to processing, I'd like to ensure that the object contains a property such as "IP Address". I would have expected that something like the following would work:

code:
$computers = Import-CSV -Path ".\list.csv"

if(-not $computers."IP Address") {
    Write-Error 'CSV File does not contain the column for "IP Address".'
}
That doesn't work nor does anything else I've tried. Something like -not $computer[0]."IP Address" works, but has the downside of returning as true if the first line of a CSV happens to have a blank IP address.
Try this as long as you know there's at least one row:
code:
"IP Address" -notin $computers[0].psobject.properties.name

quote:

Lastly, is there a way to get a function that's expecting open a file and do something with the data to actually take data from a string? For example, I have CSV files that have a first line which defines the separator character (IE. "sep=,") before the actual CSV data, which breaks Import-CSV. I've resorted to importing the file as a string, removing the first line, resaving it to a temporary file, importing the temp file with Import-CSV, and deleting the temp file. I'd much rather just skip saving and deleting the temp file and just send the string straight to Import-CSV, but I can't find a way to do that.
You can probably use a string if you use convertfrom-csv instead of import-csv.

Adbot
ADBOT LOVES YOU

Lum
Aug 13, 2003

i am a moron posted:

Probably not a powershell thing. Sounds like DNS, specifically you have a server that is unable to reach the other domain cause it’s not pointing to the right place or the search suffix isn’t the same on all of them.

Both servers are in the same subnet with the same firewall rules, and I can RDP to both of them using my DomainA\Admin account. Neither of them have domainA.local in their DNS search suffixes. Both of them are able to contact the DomainA DCs on the same port numbers (445 etc.)

WinRM config appears to be identical on both.

Server4 is supposed to be identical to Server3 as it's a failover copy.

Not sure what else to check, and unlike SSH, there's no verbose logging option I can find to see what's going on, just the generic error message.

Any idea where else to look?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply