FTP Authorisation Rules convertto-json

Hi,

Im trying to get ftp authorisation rules and export them to json to compare with settings in a json to verify.
This is a part of my json:

"Add": [
    {
        "users": "",
        "roles": "Ftp",
        "permissions": "Read, Write"
    },
    {
        "users": "",
        "roles": "FtpBackupUser",
        "permissions": "Read"
    },
    {
        "users": "FtpRolUser",
        "roles": "",
        "permissions": "Read"
    },
    {
        "users": "FtpMeetingMobileUser",
        "roles": "",
        "permissions": "Read"
    },
    {
        "users": "FtpDocBeheerUser",
        "roles": "",
        "permissions": "Read"
    },
    {
...
$AuthorisationRuleOnDir = 
@(
    Get-WebConfiguration -filter /system.ftpserver/security/authorization -Location "Ftp Site" -PSPath 'MACHINE/WEBROOT/APPHOST' | 
    Foreach { 
        $_.GetCollection() | 
        Foreach { $_.Attributes | 
            Format-Table name, value -HideTableHeaders } 
    }
)

This is the result:

PS C:\WINDOWS\system32> $AuthorisationRuleOnDir
accessType      0
users            
roles         Ftp
permissions     3



accessType              0
users                    
roles       FtpBackupUser
permissions             1



accessType           0
users       FtpRolUser
roles                 
permissions          1



accessType                     0
users       FtpMeetingMobileUser
roles                           
permissions                    1

If I want to convert it it creates this a lot of them:

PS C:\WINDOWS\system32> $AuthorisationRuleOnDir | ConvertTo-Json
[
    {
        "pageHeaderEntry":  null,
        "pageFooterEntry":  null,
        "autosizeInfo":  null,
        "shapeInfo":  {
                          "hideHeader":  true,
                          "repeatHeader":  false,
                          "tableColumnInfoList":  "Microsoft.PowerShell.Commands.Internal.Format.TableColumnInfo Microsoft.PowerShell.Commands.Internal.Format.TableColumnInfo",
                          "ClassId2e4f51ef21dd47e99d3c952918aff9cd":  "e3b7a39c089845d388b2e84c5d38f5dd"
                      },
        "groupingEntry":  null,
        "ClassId2e4f51ef21dd47e99d3c952918aff9cd":  "033ecb2bc07a4d43b5ef94ed5a35d280"
    },
    {
        "shapeInfo":  null,
        "groupingEntry":  null,
        "ClassId2e4f51ef21dd47e99d3c952918aff9cd":  "9e210fe47d09416682b841769c78b8a3"
    },
    {
        "formatEntryInfo":  {
                                "formatPropertyFieldList":  "Microsoft.PowerShell.Commands.Internal.Format.FormatPropertyField Microsoft.PowerShell.Commands.Internal.Format.FormatPropertyField",
                                "multiLine":  false,
                                "ClassId2e4f51ef21dd47e99d3c952918aff9cd":  "0e59526e2dd441aa91e7fc952caf4a36"
                            },
        "outOfBand":  false,
        "writeStream":  0,
        "ClassId2e4f51ef21dd47e99d3c952918aff9cd":  "27c87ef9bbda4f709f6b4002fa4af63c"
    },
    {
...

I tried different approaches but never get the correct layout for a descent json:

$AuthorisationRuleOnDir = (
    Get-WebConfiguration -filter /system.ftpserver/security/authorization -Location "Ftp Site" -PSPath 'MACHINE/WEBROOT/APPHOST' | 
    Foreach { $_.GetCollection() }
)
PS C:\WINDOWS\system32> $AuthorisationRuleOnDir
Attributes     : {accessType, users, roles, permissions}
ChildElements  : {}
ElementTagName : add
Methods        : 
Schema         : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

Attributes     : {accessType, users, roles, permissions}
ChildElements  : {}
ElementTagName : add
Methods        : 
Schema         : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

Attributes     : {accessType, users, roles, permissions}
ChildElements  : {}
ElementTagName : add
Methods        : 
Schema         : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

Attributes     : {accessType, users, roles, permissions}
ChildElements  : {}
ElementTagName : add
Methods        : 
Schema         : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema
....
$AuthorisationRuleOnDirJSON = (
    $AuthorisationRuleOnDir | 
    ForEach-Object { 
        (Foreach { $_.Attributes | Foreach { $_.name + """: """ + $_.value } } | ConvertTo-Json).Replace('\', '') 
    }
)
PS C:\WINDOWS\system32> $AuthorisationRuleOnDirJSON
[
    "accessType": "0",
    "users": "",
    "roles": "Ftp",
    "permissions": "3"
]
[
    "accessType": "0",
    "users": "",
    "roles": "FtpBackupUser",
    "permissions": "1"
]
[
    "accessType": "0",
    "users": "FtpRolUser",
    "roles": "",
    "permissions": "1"
]
[
    "accessType": "0",
    "users": "FtpMeetingMobileUser",
    "roles": "",
    "permissions": "1"
]
[
...

So how can I export it to json like the example

Davy,
Welcome to the forum. :wave:t4:

Before we proceed - please go back, edit your question and fix the formatting of the code.

To post code, sample data, console output or error messages you use the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thanks in advance

1 Like

Please format ALL sample data AND code as code.

One of the first things answerers do when examining a question’s code for errors or functionality is to copy and to try running the code to see what happens. This is much easier or only properly possible when you format the code.

Thanks

1 Like

Ive put all with </>

OK, I did it. :smirk:

Looking at your code and without having experiences with ftp or web configs or even without having access to it to test I see a potential issue. When you create your variable $AuthorisationRuleOnDir you use Format-Table. Format cmdlets are meant to be used ONLY for output stupid pixels to your console. If you want to use the data later on - as you do when you save it to a variable - you should NOT use format cmdlets as they actually destroy the objects.

1 Like

ok thx, i remove the format, result is this again, how can I make it from this into a json with only the name and value itself (as shown before)?

$AuthorisationRuleOnDir = @(Get-WebConfiguration -filter /system.ftpserver/security/authorization -Location "Ftp Site" -PSPath 'MACHINE/WEBROOT/APPHOST' | Foreach{$_.GetCollection() | Foreach {$_.Attributes} })

PS C:\WINDOWS\system32> $AuthorisationRuleOnDir

IsInheritedFromDefaultValue : False
IsProtected                 : False
Name                        : accessType
TypeName                    : System.UInt32
Schema                      : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value                       : 0
IsExtended                  : False

IsInheritedFromDefaultValue : True
IsProtected                 : False
Name                        : users
TypeName                    : System.String
Schema                      : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value                       : 
IsExtended                  : False

IsInheritedFromDefaultValue : False
IsProtected                 : False
Name                        : roles
TypeName                    : System.String
Schema                      : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value                       : Ftp
IsExtended                  : False

IsInheritedFromDefaultValue : False
IsProtected                 : False
Name                        : permissions
TypeName                    : System.UInt32
Schema                      : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value                       : 3
IsExtended                  : False

I cannot tell you how to get what you need for your particular case. But in general - you limit the output of objects and properties of the ones you need …

Try this:

Get-Service | 
    Select-Object -First 3 | 
        ConvertTo-Json

This will output the JSON data representing the first 3 services on your computer with all their properties.

If you do not need ALL properties you simply limit it to what you need:

Get-Service | 
    Select-Object -First 3 -Property DisplayName,Status,StartType | 
        ConvertTo-Json

Hi thanks already for your answers,

I did that previous too and problem is gives the propertynames in the json instead of the values
$AuthorisationRuleOnDir | select-object -Property name, value

Name                       Value
----                       -----
accessType                     0
users                           
roles                        Ftp
permissions                    3
accessType                     0
users                           
roles              FtpBackupUser
permissions                    1
accessType                     0
users                 FtpRolUser
roles                           
permissions                    1
accessType                     0
users       FtpMeetingMobileUser
roles                           
permissions                    1
accessType                     0
users           FtpDocBeheerUser
roles

with convertto-json i get this:

[
    {
        "Name":  "accessType",
        "Value":  0
    },
    {
        "Name":  "users",
        "Value":  ""
    },
    {
        "Name":  "roles",
        "Value":  "Ftp"
    },
    {
        "Name":  "permissions",
        "Value":  3
    },
    {
        "Name":  "accessType",
        "Value":  0
    },
    {
        "Name":  "users",
        "Value":  ""
    },
    {
        "Name":  "roles",
        "Value":  "FtpBackupUser"
    },

and not this format with value of name and value of value:
(nothing to do with numeric value permissions vs string, thats something else i want to replace)

               "Add": [
                  {
                    "users": "",
                    "roles": "Ftp",
                    "permissions": "Read, Write"
                  },
                  {
                    "users": "",
                    "roles": "FtpBackupUser",
                    "permissions": "Read"
                  },
                  {
                    "users": "FtpRolUser",
                    "roles": "",
                    "permissions": "Read"
                  },
                  {
                    "users": "FtpMeetingMobileUser",
                    "roles": "",
                    "permissions": "Read"
                  },
                  {
                    "users": "FtpDocBeheerUser",
                    "roles": "",
                    "permissions": "Read"
                  },
type or paste code here
````#Convert to JSON
$dict = [ordered]@{
    users = $AuthorisationRuleOnDir.users
    roles = $AuthorisationRuleOnDir.roles
    permisions = $AuthorisationRuleOnDir.permisions
      
    }
    $dict | ConvertTo-Json -Depth 100`