Cannot get Powershell IF statement to work.

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Hi guys, Powershell newbie here.

I am working at a simple PowerShell script which checks for some shares, and creates them if they aren't there. Should be simple, but I cannot get it to work.

Here is the code:

Code:
#Load WIN32_share class for creating shares. Place it in $Shares variable
$Shares=[WMICLASS]”WIN32_Share”

#Create Config share if it isn't already there
If (!(GET-WMIOBJECT Win32_Share -filter “name=’Config’”) {
$Shares.Create("c:\test\Config",”Config”,0) 
}

I get:

Unexpected token '{' in expression or statement.
At :line:5 char:59
+ If (!(GET-WMIOBJECT Win32_Share -filter “name=’Config’”) { <<<<

If I do not check for shares, but simply create them, there is no problem.
 

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Thanks,

I spotted that myself literally the same second you posted :)
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You should find an editor that helps with little things like that. For instance, vim automatically highlights the other end of a paren, brace or bracket when you put the cursor on one of them and you can jump back and forth by hitting %.
 

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Hmm, another error:

Code:
Get-WmiObject : Invalid query 
At C:\Users\joe\AppData\Local\Temp\32f92e9c-61ab-4eb1-a699-1ac0736b01c4.ps1:5 char:20
+ If (!(GET-WMIOBJECT <<<<  Win32_Share -filter &#8220;name=&#8217;Config&#8217;&#8221;)) {
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Am I missing something between GET-WMIOBJECT and Win32_Share?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Do you literally have <<<< in there? I ran the Get-WMIObject command by itself and it works fine without the <<<< in it. And you know that the filter string appears to match whole-string only? Meaning that it will only list shares with the name of 'Config', not 'Config2' or anything else.
 

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Thanks again. I use PowerGUI, and just figured out how to get it to highlight parentheses. I am a newbie, as I stated :)

There are no <<<< in my code - that comes form the error message. My code looks like this:

Code:
#Load WIN32_share class for creating shares. Place it in $Shares variable
$Shares=[WMICLASS]&#8221;WIN32_Share&#8221;

#Create Config share if it isn't already there
If (!(GET-WMIOBJECT Win32_Share -filter &#8220;name=&#8217;Config&#8217;&#8221;)) {
$Shares.Create("c:\test\Config",&#8221;Config&#8221;,0)
}

This is the error message:

Code:
Get-WmiObject : Invalid query 
At C:\Users\joe\AppData\Local\Temp\32f92e9c-61ab-4eb1-a699-1ac0736b01c4.ps1:5 char:20
+ If (!(GET-WMIOBJECT <<<<  Win32_Share -filter &#8220;name=&#8217;Config&#8217;&#8221;)) {
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

The filter string is OK, I only want to match whole string.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Heh no, I have no idea what meaning ` has in PS (it's the same as exec in perl) but it's most definitely not a single quote. =) I thought it looked a little funny in here, but assumed it was just a font issue.
 

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Can I set permissions to the shared folders in PowerShell as well? I can set permissions for folders with get-acl, set-acl and System.Security.AccessControl.FileSystemAccessRule, but how do I do this with shares?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I don't know off the top of my head because I try to avoid PS whenever possible, but I would first question your desire to use share permissions at all. You should always start with Everyone:FC on shares and limit access with NTFS ACLs.