Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.


Go Back   Vista Forums > Misc Newsgroups > PowerShell

RB

Vista - Creating new functions using new-item cmdlet

Reply
 
06-19-2006   #1
dreeschkind


 
 

Creating new functions using new-item cmdlet

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 System SpecsSystem Spec
06-19-2006   #2
DBMwS


 
 

RE: Creating new functions using new-item cmdlet

"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 System SpecsSystem Spec
06-19-2006   #3
dreeschkind


 
 

RE: Creating new functions using new-item cmdlet

"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 System SpecsSystem Spec
06-19-2006   #4
DBMwS


 
 

RE: Creating new functions using new-item cmdlet

> 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 System SpecsSystem Spec
06-19-2006   #5
DBMwS


 
 

RE: Creating new functions using new-item cmdlet

> 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 System SpecsSystem Spec
06-19-2006   #6
dreeschkind


 
 

RE: Creating new functions using new-item cmdlet

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 System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: Creating new functions using new-item cmdlet
Thread Forum
Problem creating private functions PowerShell
Creating objects with variable names in functions PowerShell
Creating cmdlet help PowerShell
Conflicting behaviour of rename-item cmdlet PowerShell
Parameter names for a New-Process Cmdlet: item name and working directory PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd