Run elevated Powershell Script and import system modules - windows

I'm trying to call a .PS1 using a batch file to produce a csv file with just the User Name and Other Telephone number details. I have the script to produce the csv file.
Get-ADUser -Filter * -Properties otherTelephone |
select name, #{L='otherTelephone'; E={$_.otherTelephone[0]}} | sort-object otherTelephone | select-object -last 1000 |
Export-Csv C:\Test.csv -NoTypeInformation
and I have the batch file to elevate the PowerShell
powershell -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file C:\Test.ps1' -verb RunAs}"
The problem is when I try to import the system modules by adding
powershell.exe -ImportSystemModules
to the front of the powershell script, the CSV only returns the header information e.g. name and otherTelephone. The script works if I import the modules manually i.e right click import system modules, but not when I try to load modules before running the script.
Basically I need to run the PS script as admin, import the system modules and have the code output my data.
Any help as to where I am going wrong is appreciated.
powershell.exe -ImportSystemModules Get-ADUser -Filter * -Properties otherTelephone |
select name, #{L='otherTelephone'; E={$_.otherTelephone[0]}} | sort-object otherTelephone | select-object -last 10 |
Export-Csv C:\Test.csv -NoTypeInformation

If you need to load the modules inside your script use the following code:
Get-Module -ListAvailable | Where-Object {$_.Path -like "$PSHOME*"} | Import-Module
The -ImportSystemModules switch is a flag for powershell.exe. If you call powershell.exe -ImportSystemModules inside of your script it will start another powershell instance and load the modules inside of it.
You could also add the -ImportSystemModules to your powershell call inside the batch file. That should work too
Regards

Related

Automatically export a OneNote section to PDF

I have a .one OneNote section file that I would like to automatically export to PDF at regular intervals. In Powershell 7, I discovered this command:
Start-Process "C:\path\to\ONENOTE.EXE" -ArgumentList "/print `"C:\path\to\my_section.one`""
This opens OneNote and the save location dialog for the PDF, but I can't figure out how to provide input to this dialog at the command line. I'm not sure if there's an appropriate module - I looked at https://www.powershellgallery.com/ and couldn't find one. Any ideas on how to extend the above command, or another solution entirely?
As per my comment. Your use case could be as simple as this:
$CurrentDefaultPrinter = (
Get-CimInstance -Class Win32_Printer |
Where-Object {$PSItem.Default -eq $true}
).Name
$CurrentDefaultPrinter
Get-Printer |
Where-Object {$PSItem.Name -Match 'pdf'} |
Set-Printer -Name $PSItem.Name
Start-Process 'C:\path\to\ONENOTE.EXE' -FilePath 'C:\path\to\my_section.one' -Verb Print
Set-Printer -Name $CUrrentDefaultPrinter

How to use Remove-Item -Confirm:$false -Force properly in Windows PowerShell

So, I am writing this script in PowerShell and I am required to delete a few files in APPDATA on Windows. I wrote this line of code and it doesn't remove the item silently. It asks for confirmation even after using $Confirm:false. How do I fix this issue?
My code:
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\blob_storage" | Remove-Item -Confirm:$false -Force
I get this unwanted confirmation box every time I run the script:
Here is your modified code. I hope it will work for you.
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\blob_storage" | Remove-Item -Recurse -Force

Remote Installation of Python using Powershell fails using Invoke-Command

I am trying to use this script to install Python on the remote computer. If I run this file directly on the server. This is the Python_Pip_Proxy_PyWinAuto.ps1 file. It works.
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Write-Host("Hi")
$installer="C:\temp\python-3.6.2.exe"
& $installer /quiet PrependPath=1 InstallAllUsers=1 TargetDir="C:\Python36"
However if I run the Invoke-Command using the following script to run this remotely on the same server, It print's the Hi message so I know that the file is running but Python doesn't get installed.
# Getting the list of servers from a .txt file to an array #
$SRVListFile = "C:\Scripts\ServerList.txt"
$SRVList = Get-Content $SRVListFile -ErrorAction SilentlyContinue
# Copying the .exe file from a shared location to each server in the array #
# Invoking the .ps1 file which runs natively on each server #
Foreach($computer in $SRVList) {
Get-Service remoteregistry -ComputerName $computer | start-service
Copy-item -Path "E:\Software\python-3.6.2.exe" -Destination \\$computer\c$\temp -Recurse
Copy-item -Path "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_GUI.py" -Destination \\$computer\c$\temp -Recurse
Invoke-Command -ComputerName $computer -FilePath "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_Pip_Proxy_PyWinAuto.ps1"
}
What is going wrong. What should I change the code to?
Try using the -scriptblock {Your command here} parameter to execute the command inside the scriptblock parenthesis on the remote computer.
Perhaps you can do it like
$Scriptblock = {
PowerShell -file "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_Pip_Proxy_PyWinAuto.ps1"
"This is Working" | out-file "C:\Hi.txt"
}
Invoke-Command -ComputerName $computer -Scriptblock $Scriptblock
You might want to remove the Write-Host "Hi" part because that gives the script an interactive nature. If you want to check for execution on remote computer, you can use out-file cmdlet to create a file on the remote computer as an indication.

Check if specific programs are installed on a remote server using powershell

I am writing a script where I need to check specific applications if installed or not on a remote server. I am fairly new to PowerShell, but here is the script I have written
ForEach ($computers in $computer){
Get-WMIObject -Class win32_product -Filter {Name like "%Microsoft%"} -ComputerName $computers -ErrorAction STOP | Select-Object -Property Name,Version | export-csv "C:\progrms.csv"
}
The problem I am facing here is I can get details of only Microsoft application which are installed and if I give multiple names for filter parameter like "Microsoft", "SQL", "app1" so on. This script does not seem to work.
Please let me know what has gone wrong and what needs to be done in order to get the list of specific software's that are installed. Also, note that I will be using this script for both Windows 2008 and 2012 servers.
Remove the -Filter {Name like "%Microsoft%"} part of the script to have it return all installed software.
You probably want to be doing ForEach ($computer in $computers) rather than the other way around, assuming you're creating a $computers variable somewhere above this code with the list of computer names.
You also need to -Append to the Export-CSV command as otherwise each computers output will overwrite the output of the previous, however another issue you'll then have is knowing which software comes from which computer. You can address this by using a Calculated Property to add in the computer name to the output, as follows:
ForEach ($Computer in $Computers){
Get-WMIObject -Class win32_product -ComputerName $Computer -ErrorAction Stop |
Select-Object -Property #{N='Computer';E={$Computer}},Name,Version |
Export-Csv "C:\progrms.csv" -Append
}

Accessing Remote Registry for the currently logged in user with Powershell

I'm trying to find the printer ports that are in use for the user that is logged in to multiple machines. However, when I run my script it is running Regedit as myself... How do I go about getting it for the user that is logged in?
Here is my current script:
Get-Content -Path c:\ListOfComputers.txt | ForEach-Object {
Get-ItemProperty -Path Registry::"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\" | ForEach-Object {Get-ItemProperty $_.pspath} | Format-List | Out-file c:\PortResults.txt
}

Resources