• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Creating new functions using new-item cmdlet

D

dreeschkind

#1
Why can't the new-item cmdlet just ask the user for the "value" argument if
it needs it to create an item?
I think it would be cool if cmdlets like this would also work
'interactively' without knowing all arguments/parameters in advance.


PoSh 5 [137/] Function:\>new-item

cmdlet new-item at command pipeline position 1
Supply values for the following parameters:
Path[0]: testf
Path[1]:
New-Item : Cannot process argument because the value of argument "value" is
null.
At line:1 char:8
+ new-item <<<<



PoSh 6 [137/] Function:\>new-item -value {write-host "test"}

cmdlet new-item at command pipeline position 1
Supply values for the following parameters:
Path[0]: testf
Path[1]:

CommandType Name
Definition
----------- ----
----------
Function testf
write-host "test"
 

My Computer

D

DBMwS

#2
"dreeschkind" wrote:

> Why can't the new-item cmdlet just ask the user for the "value" argument if
> it needs it to create an item?
> I think it would be cool if cmdlets like this would also work
> 'interactively' without knowing all arguments/parameters in advance.
>
>
> PoSh 5 [137/] Function:\>new-item
>
> cmdlet new-item at command pipeline position 1
> Supply values for the following parameters:
> Path[0]: testf
> Path[1]:
> New-Item : Cannot process argument because the value of argument "value" is
> null.
> At line:1 char:8
> + new-item <<<<


I am not sure why you were getting that "value is null" error because when I
just pressed "enter" at "path[1]:", New-Item asked me for "type" of New item
to create like the following:
[^_^]PS[53]>New-Item
cmdlet New-Item at command pipeline position 1
Supply values for the following parameters:
Path[0]: testf
Path[1]:
Type: file
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Program
Files\Windows PowerShell\v1.0
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 06/19/2006 오후 12:50 0 testf
[^_^]PS[54]>


>
> PoSh 6 [137/] Function:\>new-item -value {write-host "test"}
>
> cmdlet new-item at command pipeline position 1
> Supply values for the following parameters:
> Path[0]: testf
> Path[1]:
>
> CommandType Name
> Definition
> ----------- ----
> ----------
> Function testf
> write-host "test"


And the reason that "New-Item" doesn't ask for "value" is because,
PowerShell prompts user for an input only for mandatory parameters. If you
look at "new-item -?", "-Value" parameter's not required

-Value <System.Object>
The value the new item is to have.

Parameter required? false

So I think what you should be suggesting is, "-Value" parameter should be
mandator... ;)
But I don't think that should be the case since, under file system provider,
one should be able to create a file or a directory any values...
 

My Computer

D

dreeschkind

#3
"DBMwS" wrote:

> I am not sure why you were getting that "value is null" error because when I
> just pressed "enter" at "path[1]:", New-Item asked me for "type" of New item
> to create like the following:


The reason seems to be that you created a FileSystem item. It works for me
the same.

> And the reason that "New-Item" doesn't ask for "value" is because,
> PowerShell prompts user for an input only for mandatory parameters. If you
> look at "new-item -?", "-Value" parameter's not required
>
> -Value <System.Object>
> The value the new item is to have.
>
> Parameter required? false
>
> So I think what you should be suggesting is, "-Value" parameter should be
> mandator... ;)
> But I don't think that should be the case since, under file system provider,
> one should be able to create a file or a directory any values...


I think it depends on the psProvider, which parameters are mandatory.
New items in the function: psDrive obviously need the -Value as a funtion
definition.
Thus, the help on New-Item seems to be confusing to me.

--
greetings
dreeschkind
 

My Computer

D

DBMwS

#4
> I think it depends on the psProvider, which parameters are mandatory.
> New items in the function: psDrive obviously need the -Value as a funtion
> definition.

I think you are right about new-item hehaving differently according to
providers.
When you write a custom provider, as far as i remember, you can specify
whether a parameter should be a mandatory one or not.

When i went to "function:/" drive and typed "(gcm New-Item).Definition"
(which should display New-Item parameter definition, specific to "Function"
provider)
in both parametersets, "-Value" parameter is still not a mandatory parameter.
I guess in that case, having "-Value" as a non-mandatory parameter doesn't
seem to make sense...
 

My Computer

D

DBMwS

#5
> When you write a custom provider, as far as i remember,
> you can specify whether a parameter should be a mandatory one or not.

Hm i think i mixed it up with SnapIns

> I guess in that case, having "-Value" as a non-mandatory parameter
> doesn't seem to make sense...

btw, it makes sense since you can declare a function with no body,
although that won't do anything. It seems like -Value simply conforms to
how function can be defined...
[^_^]PS[106]>function test {}
[^_^]PS[107]>test
[^_^]PS[108]>
 

My Computer

D

dreeschkind

#6
But then again, if functions don't need a value (function definition), then
it should be possible to create new functions using the New-Item cmdlet
without providing a value.

--
greetings
dreeschkind

"DBMwS" wrote:

> > When you write a custom provider, as far as i remember,
> > you can specify whether a parameter should be a mandatory one or not.

> Hm i think i mixed it up with SnapIns
>
> > I guess in that case, having "-Value" as a non-mandatory parameter
> > doesn't seem to make sense...

> btw, it makes sense since you can declare a function with no body,
> although that won't do anything. It seems like -Value simply conforms to
> how function can be defined...
> [^_^]PS[106]>function test {}
> [^_^]PS[107]>test
> [^_^]PS[108]>
>
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)