Powershell 添加内容:对路径“C:\”的访问被拒绝

Powershell 添加内容:对路径“C:\”的访问被拒绝,powershell,Powershell,你好,我有这个问题 我的问题是: PS C:\WINDOWS\system32>描述信息 添加内容:对路径“C:\”的访问被拒绝。 在C:\Untitled2.ps1:41字符:72 + ... 序列号、版本、制造商|添加内容C:\$nombre\u fichero + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:PermissionDenied:C::Strin

你好,我有这个问题

我的问题是: PS C:\WINDOWS\system32>描述信息 添加内容:对路径“C:\”的访问被拒绝。 在C:\Untitled2.ps1:41字符:72 + ... 序列号、版本、制造商|添加内容C:\$nombre\u fichero + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:PermissionDenied:C::String[Add Content],UnauthorizedAccessException +FullyQualifiedErrorId:GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.AddContentCommand

main{
    crearFichero
    escribirInfo
}
function crearFichero
    {
    #Expliqueu que fa la funció!!!
        [CmdletBinding()]
        Param(

        )
        Begin
        {
        #RES
        }
        Process
        {
            $nombre_sistema = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name
            $ip = Test-Connection $nombre_sistema -Count 1 | select IPV4Address
            $ip_replace = $ip.IPV4Address.IPAddressToString.Replace(".","_")
            $fecha = Get-Date -Format "MM_dd_yyyy"
            $ext = ".txt"
            $nombre_fichero = [System.String]::Concat($ip_replace, $fecha, $ext)
            $crear_fichero= New-Item C:\$nombre_fichero -ItemType file
        }
    }

function escribirInfo
    {
    #Expliqueu que fa la funció!!!
        [CmdletBinding()]
        Param(

        )
        Begin
        {
        #RES
        }
        Process
        {
        gwmi win32_bios | select SerialNumber, Version, Manufacturer | Add-Content C:\$nombre_fichero

        }
    }
    }

默认情况下,用户无权在C:\的根目录中创建文件:

PS C:\> (get-acl c:\).Access


FileSystemRights  : AppendData
AccessControlType : Allow
IdentityReference : NT AUTHORITY\Authenticated Users
IsInherited       : False
InheritanceFlags  : None
PropagationFlags  : None

FileSystemRights  : -536805376
AccessControlType : Allow
IdentityReference : NT AUTHORITY\Authenticated Users
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : InheritOnly

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : ReadAndExecute, Synchronize
AccessControlType : Allow
IdentityReference : BUILTIN\Users
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

欢迎阿尔贝托·马丁罗德里格斯

@Gert Jan Kraijeveld是对的。您可以作为管理员运行,或者更好地将文件保存到用户目录中的某个位置或其他合适的位置,而不是主驱动器的根目录


i、 e.C:\$nombre\u fichero->C:\Users{{{YourUserName}}\$nombre\u fichero

[1]为什么所有的powershell版本标签?请只使用适合您设置的。[2] 请显示触发错误的代码。好的,我已经完成了。您的代码中有几个问题-[1]powershell中没有使用主{}结构[2]在调用函数之前需要先定义它们。你倒过来了。[3] 函数中定义的变量是该函数的局部变量。您的第41行试图写入C:\-而不是根目录中的文件。。。到根本身。为什么?因为$nombre_fichero不存在于第二个函数中-它是在第一个函数中定义的。我是一个初学者,我不知道如何从一件事开始-一件非常简单的事。让它发挥作用。再加一件事。让他们一起工作。不要使用函数util,因为您让项目彼此协作。。。即使如此,也只能在需要时使用函数。