Does Powershell ISE cache files? [duplicate] - powershell

This question already has an answer here:
Re-signing, Reloading, and running a test function from a powershell module?
(1 answer)
Closed 6 years ago.
I've been modifying a powershell module on the fly, immediately running afterwards a script that used it and kept not seeing my updates till I restarted powershell ISE. I suppose powershell ISE is caching at least modules. How do I clear the cache or control it?

You can try to Remove-Module and Import-Module, or Import-Module -Force, or Import-Module -Refresh.
Also you can google a bunch of similar topics on SO in like 15 seconds.

Related

Does Powershell not allow variables in the path for Start-Process? [duplicate]

This question already has answers here:
How do I pass a local variable to a remote `Invoke-Command`? [duplicate]
(2 answers)
Closed 2 months ago.
I am working on a script to install software to remote PCs, and I ran into an issue I have never experienced before. I am trying to pass in the file name to Start-Process as a variable, but for some reason it does not like this, and hangs on the install. It will just sit indefinitely. The log file shows that this creates shows that the process terminated unexpectedly.
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Start-Process msiexec "/i c:\$FileName /lvx* c:\PackageManagement.log" -wait }
$FileName is test.msi which exists on the target computers C: drive as I copied it there.
I have tried using Powershell joins, I have tried using concat, I have tried setting the Start_Process command to it's own variable and using that. It just hangs. The computer is on, connected to the internet. When I replace $FileName in the code above with test.msi directly, it installs just fine.
If you use $using:FileName in place of just $FileName, that will give you what you are looking for on Powershell 3.0 or later.

Which program to edit a file in powershell and just the powershell?

I see a lot of questions about this but the answers are only about get function and replace function.
Isn't there anyway to edit a file simple a nano or vi in linux, already pre-installed in powershell?
I could think about a thousand of usecase where you don't want to use function to do that. One among many is that you are remoting on a nanoserver and you want to do several changes in a single script file.
With PowerShell ISE (pre-installed on Windows clients) and vscode with PowerShell Extension and Integrated Console, you can open files in Remote PowerShell Sessions with psEdit $filetoopen.
Example
Enter-PSSession -ComputerName nanoserver
psedit C:\nanofile.txt

Powershell check script run time and kill script if longer than x minutes [duplicate]

This question already has an answer here:
Copy-Item with timeout
(1 answer)
Closed 6 years ago.
Hello my question today is how to figure out how long a powershell script has been running for. I did find something to figure out what specific powershell script has been running by using
PowerShell -File test1.ps1
then using
Get-WMIObject -Class Win32_Process -Filter "NAME='PowerShell.EXE'"
However this, as far as i can see, does not tell me how long the powershell script has been running for. Is there a way to find how long a powershell script has been running for and kill it without writing some kind of time function inside the powershell script that is running? Also is there a way to stop that script if it has been running for longer than "x" minutes?
$time = get-date
do
{
bla-bla-bla
}
while ((Get-Date).AddMinutes(-5) -le $time)
To do something for 5 minutes

Is it possible to delete or overwrite cmdlets?

I'm working with DNS resource records in Powershell 5 using code that I inherited from the guy who was trying to do this before me. The cmdlet I am trying to use is Add-DnsServerResourceRecordA.
Part of his code has import-module certain folder\PowerShell\Modules\DnsServer. The weird thing is, it seems like as I was trying bits and pieces of the code earlier, I was able to use the add-DNSblah cmdlet. Now, after It ried running the whole script including the import-module, Powershell is saying that the cmdlet does not exist natively, and when I import the module and run it it is giving me Add-DnsServerResourceRecordA: Invalid Class.
It is my understanding that Add-DnsServerResourceRecordA should be included in my normal Powershell 5.0. Could that Import-Module have permanently damaged PS somehow? Why else would the cmdlet not show up, even in a Get-Command "dns"?
I'm pretty sure you will need the Remote Server Administration Tools (RSAT) installed to have these cmdlets available on a non-server Windows OS.
You can download them from this page: https://www.microsoft.com/en-gb/download/details.aspx?id=45520.
Not really sure why the Import-Module does not fail if the DNSServer module is not present on the system.
If RSAT are already installed, you can try to reinstall them.

Determine when a verysilent install is complete [duplicate]

This question already has answers here:
How to tell PowerShell to wait for each command to end before starting the next?
(10 answers)
Closed 7 years ago.
When I run an installation from Inno Setup with:
Installer.exe /VERYSILENT
The command immediately returns even though the install takes about 10 minutes. So, if I run:
Installer.exe /VERYSILENT
DoNextThing.exe
DoNextThing.exe runs while the installer.exe is still installing.
I would like to run some configuration after the install is successful. Right now, in powershell, I do the following:
$h = Start-job -name Installer -ScriptBlock {."Installer.exe" /VERYSILENT}
$h # the ps job control commands show this job as complete very quickly
sleep 10
$x = Get-Process -ProcessName Installer
while ($x -and ! $x.HasExited)
{
write-output "waiting ..."
sleep 10
}
# Do some configuration
Although this seems to work, I think I must be missing a better way to do this. I do not want to make it part of the installer as this configuration is just for the Jenkins test environment.
Any ideas why the powershell job management does not work for this? Am I using powershell incorrectly, or is the Installer.exe generated by Inno Setup not working well with powershell? [should I be using cmd.exe instead of powershell?]
You might have to just add a command to the RUN section in inno-setup to create a file "IamFinishedInstalling.txt" as the last thing it does.
Your powershell can then block on that file rather than try to figure out process or job statuses.
while (! (Test-Path "IamFinishedInstalling.txt")) { sleep 10 }
If the installer.exe is really returning before the install is finished, this may be the simplest thing you can try.
Why use a job at all? Just run the installer using the installer command. When the executable completes, PowerShell will continue on to the next line of the script.