Tagged Questions
0
votes
2answers
20 views
Invoke-sqlcmd with scripting variables and function parameters
I have several tables that need to be periodically cleaned (de-duplicated). I want to create a function I can call and pass in a table name as a parameter. I'm stuck on the appropriate use of back ...
0
votes
1answer
21 views
Passing DateTime as a parameter
I've read widely that I can pass in an argument to my powershell script via:
param (
[Datetime]$argument
)
I've also read that I can define a default value:
param (
[Datetime]$argument = ...
1
vote
1answer
25 views
Passing FromArgb() as a parameter to a function
I have the following function:
Function Color-Cells {
Param (
[Parameter(
Mandatory = $true,
Valuefrompipeline = $true)]
[String]$Range,
...
-1
votes
2answers
37 views
Return Values of Function in Powershell
As per the code return 0 is continue and return 1 is exit.I have two functions.Both functions are in different scripts.
Function1
{
call Function2
return 1
}
Function2
{
Stored procedure()
}
...
1
vote
3answers
63 views
Passing script parameters to a function in Powershell
My Script calls a function that needs the parameters from the calling of the scripts:
function new( $args )
{
if( $args.length -lt 8 )
{
Write-Host "Parameter Missing, requires 8 Parameters. ...
1
vote
1answer
21 views
Function parameters check
I'm writing some basic powershell libraries, and I need to check if a specific parameter is in a group of values.
In this example I define a function ALV_time with an optional parameter. If defined, ...
2
votes
1answer
107 views
Dollar signs, function keywords and script blocks in powershell
I have this powershell script:
function Func1 ($val)
{
Write-Host "$val is processed by Func1";
}
function Func2($val)
{
Invoke-Command -ScriptBlock `
${function:Func1} -ArgumentList "$val is ...
0
votes
1answer
217 views
Best practice for writing equivalent powershell scripts and functions
I have a script that takes in multiple parameters, and that I've documented with proper help comments (e.g. .SYNOPSIS, .DESCRIPTION, .PARAMETER). Several different users in my organization are going ...
-1
votes
3answers
68 views
Create alias in powershell to measure the time of a function
I have a bunch os functions in a PowerShell script binded to an alias, as:
function Run
{
# somethin that takes a while
}
set-alias r Run
I would like to measure the time of each call. To do ...
1
vote
3answers
89 views
Powershell changing variable name in foreach function
I have a script where a portion of it needs to run 3 different times, so I thought I'd try expand my limited PowerShell knowledge by calling the same code using a function rather than copying and ...
1
vote
1answer
178 views
Powershell Backup Script for multiple folders
I'm having problems with a script which should function as a Backup replacement.
This is only a part of the hole thing, the other stuff works but is dependent on it.
$datum = get-date -uformat ...
0
votes
2answers
107 views
PowerShell function return type not as expected
I have a script that accepts a string parameter :
script-that-takes-string-param.ps1
param(
[Parameter(Mandatory=$true, HelpMessage="path")]
[string]$path,
)
And I have another script ...
1
vote
1answer
49 views
Change value of a param in the midst of a function
Trying to create a PowerShell function that will output a single-line of text using multiple sets of fore and back colors. I have a switch that defines the color sets.
The function has one param that ...
0
votes
1answer
143 views
Dynamically create PowerShell scriptblock from scriptblock
I want to write a powershell function that returns a scriptblock by creating one dynamically based on a scriptblock passed in as an input parameter. I don't seem to be having much luck.
It's easy to ...
0
votes
1answer
57 views
Powershell - How do I pass a collection of object types to a function as a parameter?
Here is my code:
function Get-OSInfo {
param([string]$Computer)
$OS = gwmi -class Win32_OperatingSystem -computer $Computer
$OS | Add-Member –MemberType NoteProperty –Name OSType –Value ""
$OS.OSType ...
1
vote
1answer
409 views
Run PowerShell function from Python script
I have a need to run a PowerShell function from a Python script. Both the .ps1 and the .py files currently live in the same directory. The functions I want to call are in the PowerShell script. ...
0
votes
2answers
459 views
Storing Directory Folder Names Into Array Powershell
I am trying to write a script that will get the names of all the folders in a specific directory and then return each as an entry in an array. From here I was going to use each array element to run a ...
2
votes
3answers
158 views
Why does Powershell think I'm trying to return an object[] rather then a DataTable?
I have the following PS function:
function GetBuildData {
[System.Data.SqlClient.SqlConnection] $conn = New-Object System.Data.SqlClient.SqlConnection
[System.Data.SqlClient.SqlCommand] $cmd ...
0
votes
2answers
158 views
Powershell: Functions with switches that include arguments
So I'm trying to write a script that uses switch parameters and I also need arguments to go along with them. So for instance,
Function Foo ($X, $Z, [switch]$Y, $Yarg, [switch]$K, $Karg){
if($Y){
...
0
votes
3answers
130 views
Create a function with optional call variabls: Powershell
Hi I was wondering if anyone knew how, if there is a way, to create a parameter in a powershell function where you have to call it in order to have it considered. An example given by commandlet:
...
0
votes
2answers
69 views
Powershell alias inside function not available outside
I have a powershell function inside a file myfunc.ps1
function Set-Util ($utilPath ) {
if(Test-Path($utilPath) ){
$fullPath = Join-Path -Path $utilPath "util.exe"
set-alias MyUtil ...
0
votes
1answer
31 views
Call a function with calculated parameter
I'm new to powershell. I am calling a function I made and It's separating my two parameters in to several more. How do I make the calculated parameter actually be calculated before it's passed?
dir ...
0
votes
1answer
42 views
Passing a string type to a function
I have the following code:
cls
$input = 'Hello World'
Write-Host '$input.GetType() = ' + $input.GetType()
function some-string-function($input)
{
Write-Host 'In the function now'
Write-Host ...
1
vote
3answers
117 views
Powershell functions executed regardless of being called
I'm having trouble understanding how Powershell treats functions. In the following script all functions are called, even if I never actually call the main function. Does powershell not have a concept ...
1
vote
2answers
954 views
how to dynamically add elements to arrays in powershell
I don't have much powershell experience yet and am trying to teach myself as i go along
i'm trying to make some proof of concept code for a bigger project, the main goal here is too dynamically ...
2
votes
1answer
101 views
Stop function after “x” seconds in powershell
I currently have a script that listens on a specified port. I would like this script to stop running after 5 seconds, regardless of getting connected to or not. Is there a way that I am able to do ...
0
votes
1answer
80 views
Function parameters order in PowerShell
I have this code in one of my PowerShell scripts:
function callCommandWithArguments([String] $arg1, [String] $arg2)
{
[string]$pathToCommand = "C:\command.exe";
[Array]$arguments = "anArg", ...
0
votes
3answers
95 views
How to check for functions dependency in powershell scripts. Avoid running the same function multiple times
In my PowerShell script - one function's output is another function's input. For Eg: Function CreateReport($x) cannot run until unless the Function ParseXml($x) runs. What if a user directly runs the ...
2
votes
1answer
55 views
How to specify a function as the default of a scriptblock parameter in a PowerShell function?
Given these powershell functions where foo takes a scriptblock as parameter:
function bar($name)
{
"Hello World $name"
}
function foo([scriptblock]$fun={})
{
&$fun "Bart"
}
Is it ...
0
votes
1answer
113 views
How to alias a parameterized function as a flag in powershell script?
I have a PS script which has a few functions in it.
What I need is that I should be able to pass the specific function to the PS script as an alias or a variable flag.
For Eg: Suppose I have a ...