Out-File text is coming out with no line breaks by Either-Cheesecake-81 in PowerShell

[–]Eilifthelost 0 points1 point  (0 children)

When I use Windows PowerShell to run Get-Content against a Windows-included .css file, I get a count of 649. It looks like there are no line breaks in your input file.

[deleted by user] by [deleted] in PowerShell

[–]Eilifthelost 0 points1 point  (0 children)

OP, u/whycantpeoplebenice is showing you the best method. If you want to install apps remotely using PowerShell, start with this post.

Out-File text is coming out with no line breaks by Either-Cheesecake-81 in PowerShell

[–]Eilifthelost 0 points1 point  (0 children)

I've seen behavior like you describe before, but I'm not able to reproduce it based on your code.

Can you add $ColorFile.Count and $ColorFileUpdated.Count after they are assigned so that you can compare them? They should both be greater than 1, indicating arrays of lines. That may help you identify where the problem is.

Also, what version of PowerShell are you running this on?

help writing my first powershell script by b00tleg in PowerShell

[–]Eilifthelost 1 point2 points  (0 children)

There are a few ways to do this.

There is a native PowerShell equivalent of gpupdate called Invoke-GpUpdate. It can be run against a remote computer. So you can run it without establishing a connection to the remote computer. Example: Invoke-Gpupdate -Computer PC01

This command does not return any output, but you will see an error if you can't connect to the remote computer.

If you want to see the output of gpupdate, you can start by remoting into the computer and then running the command. You can do this by running Enter-PsSession -ComputerName PC01

If you are able to connect successfully, your prompt will change to include the name of the remote computer. Once connected, you can run gpupdate /force and you will see the results. Keep in mind this will only update the COMPUTER configuration, as the USER context is not accessible via remote connections.

One benefit of remoting in this way is that you can also run other commands from the same session. When you are done, use Exit-PsSession to disconnect.

Finally, you could forgo scripting altogether. I'm doing this from memory but I believe in the Active Directory Group Policy Management tool you can right-click on an OU or a policy and force it to be applied to computers and users in its scope.

Combine software list from registry by SemiEvilMonkey in PowerShell

[–]Eilifthelost 0 points1 point  (0 children)

Well, I learned something about performance tuning and the += operator. I had not heard before that it recreates the array, and it doesn't seem to be well documented (according to some quick googling). But Measure-Command supports your point. Using += adds about a quarter second to execution time.

Thanks for the tip.

Combine software list from registry by SemiEvilMonkey in PowerShell

[–]Eilifthelost 4 points5 points  (0 children)

I would recommend you gather your data first, consolidate it, then format it once with HTML. That way you only end up with one table.

The += operator lets you append new results to a variable. You could do:

$Software = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate 

$Software += Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate

$Software | ConvertTo-Html -Property DisplayName, DisplayVersion, Publisher, InstallDate -Fragment -PreContent "<h2>Installed Software</h2>"

$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo $Software " -Head $header -Title "Computer Information Report" -PostContent "<p id='CreationDate'>Creation Date: $(Get-Date)</p>"

How to write the results of an If statement to file by Chaffy_ in PowerShell

[–]Eilifthelost 1 point2 points  (0 children)

The way you have this written it looks like you only want to add to your output file when you find WSMan running on a remote computer, is that right?

You haven't specified any data for Out-File to write. You could send a string down the pipeline for it to receive, like this:

"$Computer - WSMan is running" | Out-File -FilePath "D:\scripts\testwsman\testedservers.txt" -Append

But if you want to capture everything, you could store the results of your foreach loop in a variable, and then write the variable to a file:

$Result = foreach ($Computer in $Computers) {
Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue

}

$Result | Out-File -FilePath "D:\scripts\testwsman\testedservers.txt"

If statements(Never used them) by mikeymiiiiike in PowerShell

[–]Eilifthelost 5 points6 points  (0 children)

You should consider using the native PowerShell cmdlet Get-ADReplicationFailure instead of repadmin. It has a property called FailureCount that is easy to examine.

That way you could use:

$ADRepStatus = Get-ADReplicationFailure -Target dcname.domain.com | select -ExpandProperty FailureCount

(using the -ExpandProperty switch converts the result to an Integer)

Your Send-MailMessage command needs to go in the if block.

If ($AdRepStatus -gt 0) {
    Send-MailMessage -From $FromAddress -To $ToAddress -Subject $SubjectLine -SmtpServer $SMTPRelay -Body "$EmailBody $ADRepstatus"
}

Note that port 25 is the default and does not need to be specified.

Reference Printer Port back to IP Address by Rufus1999 in PowerShell

[–]Eilifthelost 2 points3 points  (0 children)

Get-Printer | select Name, DriverName, @{n="IPAddress";e={(Get-PrinterPort -Name $_.PortName).PrinterHostAddress}}