Share via

Problem running a PowerShell command

Kaplan, Andrew H 226 Reputation points
2023-08-01T19:38:02.5366667+00:00

Hello.

I am running a PowerShell script that is designed to list the disk space usage of a given server, local or remote, and output the results to an html file. This file, in turn, is then emailed to its intended recipient. The URL for the script is the following:

https://sqlpowershell.wordpress.com/2013/10/01/powershell-script-to-monitor-disk-space-of-a-group-of-servers-html-formatted-email-output/comment-page-1/?unapproved=13115&moderation-hash=7c658a5dbe19bf2dda12f9506e3001df#comment-13115

Using the PowerShell ISE utility, I am able to run the following commands:

Powershell -executionpolicy bypass -File C:\Scripts\disk_space_check.ps1

C:\Scripts\disk_space_check.ps1

Get-DiskSpaceReport -ComputerList C:\Scripts\server.txt -To <email address> -From <email address> -SMTP <smtp server address>

The problem that I am encountering is the following: If I run the above commands in an elevated PowerShell terminal, only the first two commands successfully execute. The third command fails, and the following error message appears on-screen:

PS C:\Windows\system32> Get-DiskSpaceReport -ComputerList C:\Scripts\server.txt -To <email address> -From <email address> -SMTP <smtp server address>
Get-DiskSpaceReport : The term 'Get-DiskSpaceReport' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ Get-DiskSpaceReport -ComputerList C:\Scripts\server.txt -To ahkaplan@ ...
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DiskSpaceReport:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


What do I need to do in order to correct this problem? The goal is to run the above commands in a PowerShell script that, in turn, can be scheduled to run at regular intervals using the Task Scheduler utility.

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2023-08-01T21:21:30.3466667+00:00

You need to "dot-source" the script that contains the function.

. C:\Scriptsdisk_space_check.ps1    # NOTE THE PERIOD FOLLOWED BY A SPACE AT THE BEGINNING OF THIS LINE

Get-DiskSpaceReport -ComputerList C:\Scripts\server.txt -To <email address> -From <email address> -SMTP <smtp server address>

The ISE is "remembering" the code. When you run it in a PowerShell window you need to get the functions into the sessions memory before you can call them.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.